gpt4 book ai didi

t-sql - ANSI_NULLS 在 SQL 中如何工作?

转载 作者:行者123 更新时间:2023-12-03 07:03:43 24 4
gpt4 key购买 nike

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

最佳答案

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

SET ANSI_NULLS ON affects a comparison only if one of the operands of the comparison is either a variable that is NULL or a literal NULL. If both sides of the comparison are columns or compound expressions, the setting does not affect the comparison.

关于t-sql - ANSI_NULLS 在 SQL 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2866714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com