正如您在下图中看到的,如果我执行以下查询,我会得到至少10个结果:
SELECT TOP (10) [t0].[u_nonreportable]
FROM [sdidataitem] AS [t0]
WHERE [t0].[u_nonreportable] IS NULL
Run Code Online (Sandbox Code Playgroud)
相比之下,如果我只是执行这样的查询,我得不到任何结果:
-- Region Parameters
DECLARE @p0 NVarChar(1000) = 'Y'
-- EndRegion
SELECT TOP (10) [t0].[u_nonreportable]
FROM [sdidataitem] AS [t0]
WHERE [t0].[u_nonreportable] <> @p0
Run Code Online (Sandbox Code Playgroud)
如果有[u_nonreportable]为空的结果,那么当我说[u_nonreportable]!='Y'时,为什么我得不到任何结果呢?
注意:我在上面的例子中使用LINQPad; 但是,我还通过在SSMS中运行SQL查询来确认结果.
答案在于NULLSQL Server 中s 的语义.这两个NULL = NULL和NULL != NULL是假的.所以,当你说[u_nonreportable] != 'Y',结果是假[u_nonreportable]的NULL.
更具体地说,当ANSI_NULLS设置为时,上述语义为真ON,这是默认值,很快就是唯一值.