neoo neoo - 2 months ago 6
SQL Question

Script to change Null values for all columns in table

Hi I am looking for SQL Server script or query which makes
Null values to user defined default values for all columns in a table.

I know we can do column by column but my table contains 300 columns.

Thanks in advance..

Answer

I have re-written my query to set the values to 0.

SELECT 'UPDATE [Your Table] SET ' 
        + c.name + ' = COALESCE(' 
        + c.name + ',''0'')'  FROM sys.columns AS c
JOIN sys.tables AS t ON t.object_id = c.object_id
WHERE t.name = '[Your table]'