ANSI_NULLS如何在TSQL中工作?

Ros*_*ant 4 t-sql null sql-server-2008

SET ANSI_NULLS OFF似乎在TSQL中给出不同的结果,具体取决于您是在比较表中的字段还是值.任何人都可以帮助我理解为什么我的最后两个查询没有结果?我不是在寻找解决方案,只是一个解释.

select 1 as 'Col' into #a
select NULL as 'Col' into #b

--This query gives results, as expected.  
SET ANSI_NULLS OFF
select * from #b
where NULL = Col

--This query gives results, as expected.
SET ANSI_NULLS OFF
select * from #a
where NULL != Col

--This workaround gives results, too.
select * from #a a, #b b
where isnull(a.Col, '') != isnull(b.Col, '')

--This query gives no results, why?
SET ANSI_NULLS OFF
select * from #a a, #b b
where a.Col != b.Col

--This query gives no results, why?
SET ANSI_NULLS OFF
select * from #a a, #b b
where b.Col != a.Col
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 10

最后两个查询失败的原因是SET ANSI_NULLS ON/OFF仅在与变量或NULL值进行比较时才适用.在比较列值时不适用.来自BOL:

仅当比较的一个操作数是NULL或文字NULL的变量时,SET ANSI_NULLS ON才会影响比较.如果比较的两侧都是列或复合表达式,则该设置不会影响比较.