有没有一种快速的方法来检查任何列是否为 NULL? [英] Is there a quick way to check if ANY column is NULL?

查看:20
本文介绍了有没有一种快速的方法来检查任何列是否为 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;

这篇关于有没有一种快速的方法来检查任何列是否为 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆