为什么"WHERE column = NULL"在SQL Server中抛出错误?

Rya*_*ill 1 sql sql-server sql-server-2005 sql-server-2008

可能重复:
在where子句+ SQL Server中IS NULL vs = NULL

考虑以下代码 - 有人可以向我解释为什么一个查询不会抛出错误?我可以理解为什么它没有返回记录,但是它想要做什么?请记住,colA是一个int.它在2000,2005和2008 r2上的工作原理相同

create table #foo
( colA int )


insert into #foo
(colA)
values
(null)

select * from #foo --returns the one record we just created

select * from #foo where colA = null --does not throw an error and does not return a record! why??
select * from #foo where colA is null --returns the record

drop table #foo
Run Code Online (Sandbox Code Playgroud)

这个表中是否存在可以返回colA = null的记录?

我目前没有任何其他数据库供我试用 - 这是标准还是特定于MSSQL的行为?

Cha*_*ndu 5

这是因为NULL不能等同于任何值.

禁用ANSI_NULLS选项,然后运行它,您将立即看到该行:

SET ANSI_NULLS OFF
select * from #foo --returns the one record we just created  
select * from #foo where colA = null --does not throw an error and does not return a record! why?? 
select * from #foo where colA is null --returns the record  drop table #foo 
Run Code Online (Sandbox Code Playgroud)