为什么 SQL 团队引入了 SET ANSI_NULLS ON
Why the SQL Team introduced SET ANSI_NULLS ON
我读了一篇有趣的文章here
它告诉我们,当写入 SET ANSI_NULLS ON
时,SQL 服务器以不同的方式对 NULL 值进行比较。网站给出的例子如下:
SET ANSI_NULLS ON IF NULL = NULL PRINT 'same' ELSE PRINT 'different'
--result: different
SET ANSI_NULLS ON IF NULL IS NULL PRINT 'same' ELSE PRINT 'different'
-- result: same
虽然我能看懂文字,但我看不懂why did the SQL have to behave this way ?
- 为什么我们不能直接使用
=
。其背后的意图是什么?为什么我们被迫使用 IS NULL
种语法?
- 在
sql server
的未来版本中,set ansi-nulls on
似乎将成为默认值。
这背后肯定是有原因的。谁能给我解释一下?
根据@Blams 的回答,我尝试了下面的代码。无论我们设置 ansi nulls to on or off
.
还是一样的结果
create table A
(
id int identity not null,
name varchar null
)
create table B
(
id int identity not null,
name varchar null
)
insert into A (name) values ('a')
insert into B (name) values ('b')
insert into A (name) values (null)
insert into B (name) values (null)
set ansi_nulls off
select A.name, B.name from A join B on A.name = B.name
set ansi_nulls on
select A.name, B.name from A join B on A.name = B.name
结果集:
--None--
两者似乎都是 returning 空行。如果我执行 SET ANSI_NULLS OFF
,它应该比较 null=null
和 return 为真,对吗?那为什么 return 没有结果?
如果你想使用 = 则只需将 ANSI_NULLS 设为 OFF。
如果我在 a.name = b.name 上进行连接,我几乎不想在 null 上进行匹配。
我读了一篇有趣的文章here
它告诉我们,当写入 SET ANSI_NULLS ON
时,SQL 服务器以不同的方式对 NULL 值进行比较。网站给出的例子如下:
SET ANSI_NULLS ON IF NULL = NULL PRINT 'same' ELSE PRINT 'different'
--result: different
SET ANSI_NULLS ON IF NULL IS NULL PRINT 'same' ELSE PRINT 'different'
-- result: same
虽然我能看懂文字,但我看不懂why did the SQL have to behave this way ?
- 为什么我们不能直接使用
=
。其背后的意图是什么?为什么我们被迫使用IS NULL
种语法? - 在
sql server
的未来版本中,set ansi-nulls on
似乎将成为默认值。
这背后肯定是有原因的。谁能给我解释一下?
根据@Blams 的回答,我尝试了下面的代码。无论我们设置 ansi nulls to on or off
.
create table A
(
id int identity not null,
name varchar null
)
create table B
(
id int identity not null,
name varchar null
)
insert into A (name) values ('a')
insert into B (name) values ('b')
insert into A (name) values (null)
insert into B (name) values (null)
set ansi_nulls off
select A.name, B.name from A join B on A.name = B.name
set ansi_nulls on
select A.name, B.name from A join B on A.name = B.name
结果集: --None--
两者似乎都是 returning 空行。如果我执行 SET ANSI_NULLS OFF
,它应该比较 null=null
和 return 为真,对吗?那为什么 return 没有结果?
如果你想使用 = 则只需将 ANSI_NULLS 设为 OFF。
如果我在 a.name = b.name 上进行连接,我几乎不想在 null 上进行匹配。