有没有一种快速的方法来检查任何列是否为 NULL?

问题描述:

我有一个大约有 20 列的表格.除了打字:

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...

是否有一种更快捷的方法来检查每一列并查看是否有任何值为 null,如果是,则返回该记录?

Is there a quicker way to just check every column and see if any value is null and if so, return that record?

没有.有一些方法可以更快地对其进行编码,但没有您暗示的捷径.摘自 我在 dba.stackexchange 上给出的答案::>

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;