日期:2014-05-18  浏览次数:20510 次

大家讨论下这个sql中is null 和 = null的区别!进者有分
比方:

select * from where b is null与
select * from where b = null

他们的结果相同吗?



------解决方案--------------------
进者有分
------解决方案--------------------
=null 语法不过
------解决方案--------------------
不一样,楼主为何不测试一下呢.
------解决方案--------------------
is null 是正确的, 

null 不等于任何数值,简单的说 null 不等于 null
------解决方案--------------------
在SQL中判断是不是空的语句只有is null和is not null两种.楼主你给出的第2句话本身应当是错误的,不会返回任何记录的应该.
------解决方案--------------------
探讨
is null 是正确的,

null 不等于任何数值,简单的说 null 不等于 null

------解决方案--------------------
null 不可等于
------解决方案--------------------
当选项SET ANSI_NULLS 设置为OFF的时候,结果是相同的
设置为ON的时候结果不同
------解决方案--------------------
declare @t table(ID int,[name] varchar(10))
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=null

ID name
----------- ----------
NULL H

(1 行受影响)

ID name
----------- ----------

(0 行受影响)

------解决方案--------------------
SQL code
当选项SET ANSI_NULLS 设置为OFF的时候,结果是相同的 
设置为ON的时候结果不同

select * from where b is null与 
select * from where b = null 
select * from where b = 'null' --当b=null字符时

------解决方案--------------------
探讨
当选项SET ANSI_NULLS 设置为OFF的时候,结果是相同的
设置为ON的时候结果不同

------解决方案--------------------
探讨
当选项SET ANSI_NULLS 设置为OFF的时候,结果是相同的
设置为ON的时候结果不同

------解决方案--------------------
探讨
is null 和 = null 語法上都是正確的。但如果要判斷是否為空,正確的用法是 is null. 樓主用= null 是不是應該用 = ''代替。

------解决方案--------------------
探讨
SQL code当选项SET ANSI_NULLS 设置为OFF的时候,结果是相同的
设置为ON的时候结果不同

select * from where b is null与
select * from where b = null
select * from where b = 'null' --当b=null字符时

------解决方案--------------------
探讨
SQL code当选项SET ANSI_NULLS 设置为OFF的时候,结果是相同的
设置为ON的时候结果不同

select * from where b is null与
select * from where b = null
select * from where b = 'null' --当b=null字符时

------解决方案--------------------
数据库选项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”的同义词。
SQL code
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 NULL

select * 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