Is there a quick way to check if ANY column is NULL?

后端 未结 4 1475
清歌不尽
清歌不尽 2020-12-06 17:15

I have a table with around 20 columns. Aside from typing out:

Where column1 is null OR column2 is null OR column3 is null etc...

Is there

4条回答
  •  一个人的身影
    2020-12-06 17:51

    No. There are ways to code it quicker, but there are no shortcuts like you imply. Taken from an answer I gave on dba.stackexchange:

    DECLARE @tb NVARCHAR(255), @sql NVARCHAR(MAX);
    
    SET @tb = N'dbo.[table]';
    
    SET @sql = N'SELECT * FROM ' + @tb + ' WHERE 1 = 0';
    
    SELECT @sql = @sql + N' OR ' + QUOTENAME(name) + ' IS NULL'
        FROM sys.columns 
        WHERE [object_id] = OBJECT_ID(@tb);
    
    EXEC sp_executesql @sql;
    

提交回复
热议问题