数据库选项ANSI nulls对应于会话设置SET ANSI_NULLS。当它设为ture时,所有与空值进行的比较都会返回FALSE。当该选项被设置为False时,如果两个值都为NULL,那么非Unicode值与空值比较将返回TURE。此外当选项被设置为TURE时,您的代码必须使用IS NULL条件来确定列是否具有NULL值。当该选项被设置为False时,SQL Server允许“= NULL”作为"IS NULL"的同义词,“< > NULL”作为“IS NOT NULL”的同义词。 create table test ( a varchar(10) null )insert test select NULL union all select '1' union all select NULL union all select '2' union all select NULLselect * from test--有多条结果返回 set ANSI_NULLS off go select * from test where a = NULL go --没有结果返回 set ANSI_NULLS on go select * from test where a = NULL go drop table test参考书《Microsoft SQL Server 2000 技术内幕》北京大学出版社 Page237第二段
设置为ON的时候结果不同
insert @t select 1,'A'
insert @t select 2,'G'
insert @t select null,'H'
insert @t select '','K'
select * from @t where id is null
select * from @t where id=nullID name
----------- ----------
NULL H(1 行受影响)ID name
----------- ----------(0 行受影响)
设置为ON的时候结果不同select * from where b is null与
select * from where b = null
select * from where b = 'null' --当b=null字符时
正解,一般情况下,默认SET ANSI NULLS 是ON的。一般都是用 IS NULL。建议按常规来。
那如果有这样的一个问题.
和这个是相关的:
关于null问题的解决:http://topic.csdn.net/u/20080821/17/350a5716-3a38-4962-bf47-1471d3e129d8.html
create table test
(
a varchar(10) null
)insert test
select NULL
union all
select '1'
union all
select NULL
union all
select '2'
union all
select NULLselect * from test--有多条结果返回
set ANSI_NULLS off
go
select * from test where a = NULL
go
--没有结果返回
set ANSI_NULLS on
go
select * from test where a = NULL
go
drop table test参考书《Microsoft SQL Server 2000 技术内幕》北京大学出版社 Page237第二段