sql – 检查任何列是否为NULL

我需要检查sql语句中的列是否为NULL.

我的SQL查询

select column_a,column_b,column_c,column_d,column_x
from myTable

我的选择中有很多列.所以我有一个性能问题,如果我会做以下:

select column_a,column_x
from myTable
where column_a is not null or column_b is not null or column_c is not null 
or column_x  is not null

是否有另一种(更好)的方式来检查是否有任何不为NULL的列?

解决方法

您可以使用COALESCE进行此操作. COALESCE返回第一个非空值(如果有).这可能不会更好,但更可读.

例:

where coalesce(column_a,column_x) is not null

根据数据的基数,您可以添加索引以帮助执行性能.

一个可能性是使用持久化的计算列,告诉您所有四列是否为NULL.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...