当字段具有NULL值时,SQL WHERE子句不返回行

Joh*_*hnB 1 c# sql datatable

好的,所以我知道这个问题:

当SET ANSI_NULLS为ON时,对空值的所有比较都计算为UNKNOWN

但是,我试图查询一个DataTable.

我可以添加到我的查询中:

OR col_1 IS NULL OR col_2 IS NULL
Run Code Online (Sandbox Code Playgroud)

每列,但我的表有47列,我构建动态SQL(字符串连接),它只是似乎是一个痛苦这样做.还有其他解决方案吗?

我要带回比较中有NULL值的所有行WHERE.

UPDATE

给我带来问题的查询示例:

string query = col_1 not in ('Dorothy', 'Blanche') and col_2 not in ('Zborna', 'Devereaux')
grid.DataContext = dataTable.Select(query).CopyToDataTable();
Run Code Online (Sandbox Code Playgroud)

(如果/当col_1 = null和/或时没有检索行col_2 = null)

Ric*_*iwi 5

所以你的意思是(例如2列)

WHERE (col1 = 'abc' or col1 is null)
  AND (col2 = 3 or col2 is null)
Run Code Online (Sandbox Code Playgroud)

但是你想要总是包含空值?这应该工作

WHERE isnull(col1,'abc') = 'abc'
  AND isnull(col2, 3) = 3
Run Code Online (Sandbox Code Playgroud)