Finding count of null values in 500 columns of a single table I have 500 columns in a table in SQL Server. I want to find the count of null values in each column. Is there a way to do it using a cursor and dynamic SQL?
An alternative that does not use a cursor:

-- Set the target table details here
DECLARE 
    @Schema sysname = N'Production',
    @Table sysname = N'Product',
    @SQL nvarchar(max) = N'SELECT ';

DECLARE
    @QName sysname = QUOTENAME(@Schema) + N'.' + QUOTENAME(@Table);

SET @SQL +=
(
    SELECT
        NCHAR(13) + NCHAR(9) +
        QUOTENAME(C.name + N'_NullCount') + ' = ' +
        N'COUNT_BIG(*) - COUNT_BIG(' +
        QUOTENAME(C.name) + N'),'
    FROM sys.columns AS C
    WHERE 
        C.[object_id] = OBJECT_ID(@QName)
        AND C.is_nullable = CONVERT(bit, 1)
        AND C.is_filestream = CONVERT(bit, 0)
        AND C.is_column_set = CONVERT(bit, 0)
    ORDER BY 
        C.column_id
    FOR XML PATH (''), TYPE
).value('.[1]/text()[1]', 'nvarchar(max)');

SET @SQL = 
    STUFF(@SQL, LEN(@SQL), 1,  NCHAR(13) + N'FROM ' + @QName + N';');

PRINT @SQL;

EXECUTE (@SQL);


The SQL generated on the AdventureWorks example given is:

SELECT 
    [Color_NullCount] = COUNT_BIG(*) - COUNT_BIG([Color]),
    [Size_NullCount] = COUNT_BIG(*) - COUNT_BIG([Size]),
    [SizeUnitMeasureCode_NullCount] = COUNT_BIG(*) - COUNT_BIG([SizeUnitMeasureCode]),
    [WeightUnitMeasureCode_NullCount] = COUNT_BIG(*) - COUNT_BIG([WeightUnitMeasureCode]),
    [Weight_NullCount] = COUNT_BIG(*) - COUNT_BIG([Weight]),
    [ProductLine_NullCount] = COUNT_BIG(*) - COUNT_BIG([ProductLine]),
    [Class_NullCount] = COUNT_BIG(*) - COUNT_BIG([Class]),
    [Style_NullCount] = COUNT_BIG(*) - COUNT_BIG([Style]),
    [ProductSubcategoryID_NullCount] = COUNT_BIG(*) - COUNT_BIG([ProductSubcategoryID]),
    [ProductModelID_NullCount] = COUNT_BIG(*) - COUNT_BIG([ProductModelID]),
    [SellEndDate_NullCount] = COUNT_BIG(*) - COUNT_BIG([SellEndDate]),
    [DiscontinuedDate_NullCount] = COUNT_BIG(*) - COUNT_BIG([DiscontinuedDate])
FROM [Production].[Product];


Execution plan:



Results (sample):