declare @alive bit;
set @alive = null;if @alive = 1 or @alive != 1
print 'Alive or Dead';
else
print 'Huh? Neither alive nor dead';
if @alive=@alive
PRINT 'It must equal itself, right?'
ELSE
PRINT 'Not always. null does not equal null';
Huh? Neither alive nor dead
Not always. null does not equal null代码挺有意思的。
set @alive = null;if @alive = 1 or @alive != 1
print 'Alive or Dead';
else
print 'Huh? Neither alive nor dead';
if null is null
PRINT 'It must equal itself, right?'
ELSE
PRINT 'Not always. null does not equal null';
/*
Huh? Neither alive nor dead
It must equal itself, right?
set @a = 0
select nullif ( @a , '' )这个更有意思
null is null!
NULL确实不能比较只能IS
declare @alive bit;
set @alive = null;if @alive = 1 or @alive != 1
print 'Alive or Dead';
else
print 'Huh? Neither alive nor dead';
if @alive=@alive
PRINT 'It must equal itself, right?'
ELSE
PRINT 'Not always. null does not equal null';--> 结果
Alive or Dead
It must equal itself, right?
摘自t-sql技术内幕
这个代码把他的意思表现的很好,我认为的。
set @alive = null;if @alive = 1 or @alive != 1
print 'Alive or Dead';
else
print 'Huh? Neither alive nor dead';
if @alive=@alive
PRINT 'It must equal itself, right?'
ELSE
PRINT 'Not always. null does not equal null';
是的,各语言不同。我今天给一个人说,他用JAVA试就都进的true。
SET ANSI_NULLS OFFdeclare @alive bit;
set @alive = null;if @alive = 1 or @alive != 1
print 'Alive or Dead';
else
print 'Huh? Neither alive nor dead';
if @alive=@alive
PRINT 'It must equal itself, right?'
ELSE
PRINT 'Not always. null does not equal null';
SET ANSI_NULLS OFF/*
Alive or Dead
It must equal itself, right?
*/
这样呢? 因为当 SET ANSI_NULLS 为 ON 时,如果比较中有一个或多个表达式为 NULL,则既不输出 TRUE 也不输出 FALSE,而是输出 UNKNOWN。这是因为未知值不能与其他任何值进行逻辑比较。这种情况发生在一个表达式与 NULL 单词进行比较,或者两个表达式相比,而其中一个表达式取值为 NULL 时。
当 ANSI_NULLS 为 OFF 时,如果 ColumnA 包含空值,则比较操作 ColumnA = NULL 返回 TRUE;如果 ColumnA 除包含 NULL 外还包含某些值,则比较操作返回 FALSE。此外,两个都取空值的表达式的比较也输出 TRUE。
declare @alive bit;
set @alive = null;if @alive = 1 or @alive != 1
print 'Alive or Dead';
else
print 'Huh? Neither alive nor dead';
if null is null
PRINT 'It must equal itself, right?'
ELSE
PRINT 'Not always. null does not equal null';
------------------------*/
Huh? Neither alive nor dead
It must equal itself, right?
if @alive = 1 or @alive != 1//当变量为NUll时任何对他的加减比较结果都是NULL,但是alive!=1不成立???怪怪的?
if @alive=@alive//然道NUll没法和自身比较?