简体   繁体   English

如何在T-SQL中检查整个表(所有列)中是否有NULL

[英]How do I check if there are any NULLs in an entire table (across all columns) in T-SQL

I'm wondering if I can run an ad-hoc query just to see if a table contains any NULL values across any of its columns. 我想知道是否可以运行临时查询只是为了查看表是否在其任何列中包含任何NULL值。 The table has 100+ columns so doing this manually would be a huge pain. 该表有100多个列,因此手动执行此操作会非常麻烦。

You can try like this: 您可以这样尝试:

DECLARE @tb NVARCHAR(255) = N'dbo.[table]';

DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM ' + @tb
    + ' WHERE 1 = 0';

SELECT @sql += N' OR ' + QUOTENAME(name) + ' IS NULL'
    FROM sys.columns 
    WHERE [object_id] = OBJECT_ID(@tb);

EXEC sp_executesql @sql;

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM