NULL不是数值

houyajin 2011-12-28 04:00:36

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

代码挺有意思的。
...全文
2589 58 打赏 收藏 转发到动态 举报
写回复
用AI写文章
58 条回复
切换为时间正序
请发表友善的回复…
发表回复
polk6 2012-01-06
  • 打赏
  • 举报
回复
null不能比较,只能判断 is null or not is null
csdaa2010 2012-01-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 houyajin 的回复:]
SQL code

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 ……
[/Quote]

if @alive = 1 or @alive != 1//当变量为NUll时任何对他的加减比较结果都是NULL,但是alive!=1不成立???怪怪的?
if @alive=@alive//然道NUll没法和自身比较?
happyerveyday 2012-01-04
  • 打赏
  • 举报
回复
/*------------------------
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?
panyufeng000 2012-01-02
  • 打赏
  • 举报
回复
这是null呀!!
偷钻石的小子 2012-01-02
  • 打赏
  • 举报
回复
怎么搞的啊的啊
qlyscf 2012-01-01
  • 打赏
  • 举报
回复
NUll不等于任何值,包括本身!!!
黄_瓜 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 42 楼 houyajin 的回复:]

引用 40 楼 beirut 的回复:

镁铝就是不一样,发个帖子都能上首页

求上墙


这个我真不知道,弱弱的问下‘镁铝’是什么意思。
[/Quote]
美女的意思。。。。。。。。。。
一十七 2011-12-30
  • 打赏
  • 举报
回复
结尾的时候写错了 SET ANSI_NULLS ON
一十七 2011-12-30
  • 打赏
  • 举报
回复

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。
ark_yk 2011-12-30
  • 打赏
  • 举报
回复
比较机制不一样,运行结果可能不一样啊
fu18701677382 2011-12-30
  • 打赏
  • 举报
回复
试着玩一下 还行
黄瓜黄瓜 2011-12-29
  • 打赏
  • 举报
回复
让我想起了 更新数据 没有加isnull 比较 引发的血案
longq121 2011-12-29
  • 打赏
  • 举报
回复
有点疯狂,有点迷茫.
忠臣 2011-12-29
  • 打赏
  • 举报
回复
null 只能用is来判断
hhloong 2011-12-29
  • 打赏
  • 举报
回复
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';

gogodiy 2011-12-29
  • 打赏
  • 举报
回复
这贴有收获~
mycodeis0000 2011-12-29
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 beirut 的回复:]

引用 18 楼 houyajin 的回复:

引用 5 楼 beirut 的回复:

SQL code
What would be the output ?

declare @a smallint
set @a = 0
select nullif ( @a , '' )


这个更有意思


见识了。

能引起注意 不容易啊,哥的回答向来不被别人重视
[/Quote]

主要是你的图片 太嚣张了 有欠扁的感觉~~


嘿嘿~~玩笑
houyajin 2011-12-29
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 w001001001001 的回复:]

Null是什么都没有的意思,所以不是''.
[/Quote]
NULL是指不存在或者未指定的词。
这个代码把他的意思表现的很好,我认为的。
Elvis_chen 2011-12-29
  • 打赏
  • 举报
回复
Null是什么都没有的意思,所以不是''.
geniuswjt 2011-12-29
  • 打赏
  • 举报
回复
学习了,不过不知道微软搞这个set干嘛用的[Quote=引用 16 楼 ap0405140 的回复:]
set ansi_nulls off后结果就不同了.

SQL code

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 n……
[/Quote]
加载更多回复(34)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧