34,593
社区成员
发帖
与我相关
我的任务
分享
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';
SET ANSI_NULLS OFF
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';
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。