更新时间:2023-12-01 09:18:16
尝试:
Try:
SELECT ... FROM MyTable
WHERE COALESCE(col4, col5, col6, col7, ...) IS NULL
虽然您可以使用COALESCE
来查找第一个非空值,我实际上看到IS NULL没有问题。例如,如果您需要动态设置条件,使用IS NULL
结构可能会更容易。
使用IS NULL
查询类似于
While you can useCOALESCE
to find the first non null value, I actually see no problem with IS NULL. For example if you need to set the conditions dynamically, usingIS NULL
structure may be easier.
UsingIS NULL
the query would be something like
SELECT Id, name, Policy, cost, salary, flag, ..., rank
FROM TableName
WHERE cost IS NULL
AND salary IS NULL
AND flag IS NULL
...
AND rank IS NULL
但是,如果这个条件设置是你经常需要的,那么你可以考虑在您的表中添加计算字段。例如,你可以有一个名为 AllItemsNull
的字段,它可能类似于
However, if this condition set is something you often need, then you could consider adding a calculated field to your table. For example you could have a field named AllItemsNull
which could be something like
ALTER TABLE TableName
ADD AllItemsNull AS CASE
WHEN COALESCE(cost, salary, flag, ..., rank) IS NULL THEN 1
ELSE 0
END
现在检查所有项是否为空的查询可以是
Now the query for checking if all items are null could be
SELECT Id, name, Policy, cost, salary, flag, ..., rank
FROM TableName
WHERE AllItemsNull = 1
附加:
链接以了解有关计算列的更多信息:
在表中指定计算列Microsoft Docs [ ^ ]