请问该执行的结果是什么?

[复制链接]
查看11 | 回复9 | 2007-10-20 08:38:44 | 显示全部楼层 |阅读模式
declare
2my_name varchar2(10) :='scott';
3other_name varchar2(10) :=null;
4begin
5if not (my_name = 'scott' and other_name = 'john') then
6dbms_output.put_line('condition_true');
7else
8dbms_output.put_line('condition_false');
9end if;
10* end;
请问结果是什么?为什么?多谢!!
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
condition_false 不符合 my_name = 'scott' and other_name = 'john'都是'condition_false
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
other_name = 'john'----> null 与其它值等于比较,返回 null
输出 false
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
null与任何值做计算或逻辑比较,结果都为null,并不能得到你要的true或false
begin
if not (null='a') then
dbms_output.put_line('True');
else
dbms_output.put_line('False');
end if;
end;
False
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
看楼主的意思,是期望if 后面那个not 能起作用,而事实上是没有起到将逻辑结果反过来的作用的
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
原因就是因为,NULL在oracle里,含义是“不确定”,而不是空,而空是可以理解为一个确定的值的,但NULL是不确定的值。
正是因此,NULL和任何值(包括NULL)比较,得出的结果还是NULL,还是不确定,而不是false(假),因此not 没有起到作用
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
哈哈,俺恰好这两天刚仔细研究了关于null的定义,有位前辈做了很精妙的总结,俺贴出来供参考,如下:~~~
与时
false and false = falsefalse and true = falsefalse and null = false
true and false = false true and true=truetrue and null = null
null and false = false null and true=nullnull and null= null
或时:
false or false = falsefalse or true = truefalse or null = null
true or false = true true and true=truetrue and null = true
null or false = null null or true=truenull or null= null
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
最初由 Laurence.li 发布
[B]看楼主的意思,是期望if 后面那个not 能起作用,而事实上是没有起到将逻辑结果反过来的作用的 [/B]

的确是这样的,我就想NOT怎么没起到作用,一下让你说中了!!
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
呵呵,我认为是false
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
最初由 fangjinde 发布
[B]null与任何值做计算或逻辑比较,结果都为null,并不能得到你要的true或false
[/B]

Null or True=True
Null and False=False
eg:
SQL> set serveroutput on
SQL> l
1begin
2if (NUll and false) is null then
3 dbms_output.put_line('NUll');
4 elsif (NUll and false) then
5dbms_output.put_line('True');
6 else
7dbms_output.put_line('False');
8end if;
9* end;
SQL> /
False
PL/SQL 祘?抖?ЧΘ.
SQL>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行