首页 新闻 会员 周边

Oracle 如何一条语句判断 ID <>'' And ID IS NOT NULL

0
[已解决问题] 解决于 2013-05-26 21:19

我就是想在存储过程中写一个 不为空 同时不为NULL的语句!

都有什么好方法呢?

 

我看到一个帖子,说:

if nvl(strsql, '') <> '' then
end if

不过那人说这个要慎重使用(原因如下):

[php]
SQL> var a varchar2(10);
SQL> exec :a := '';

PL/SQL 过程已成功完成。

SQL> select case when nvl(:a, 'null') = 'null' then 'null' else 'not null' end from dual;

CASEWHEN
--------
null

SQL> select case when nvl(:a, 'null') <> 'null' then 'not null' else 'null' end from dual;

CASEWHEN
--------
null

SQL> select case when nvl(:a, '') <> '' then 'not null' else 'null' end from dual;

CASEWHEN
--------
null

SQL> select case when nvl(:a, '') = '' then 'null' else 'not null' end from dual;

CASEWHEN
--------
not null

SQL>[/php]

问题补充:
Oracle 中,空字符串存入到Oracle中会自动转换为NULL,另外VARCHAR2把空串等同于null处理。

SQL> select 1 from dual where null=null;  没有查到记录  
SQL> select 1 from dual where null='';  没有查到记录  
SQL> select 1 from dual where ''='';  没有查到记录  
SQL> select 1 from dual where null is null; 

谢谢 @geass 的回复!
imefong的主页 imefong | 菜鸟二级 | 园豆:208
提问于:2013-05-24 15:42
< >
分享
最佳答案
0
Oracle 中,空字符串存入到Oracle中会自动转换为NULL,另外VARCHAR2把空串等同于null处理。

SQL> select 1 from dual where null=null;  没有查到记录  
SQL> select 1 from dual where null='';  没有查到记录  
SQL> select 1 from dual where ''='';  没有查到记录  
SQL> select 1 from dual where null is null; 

谢谢 @geass 的回复!
imefong | 菜鸟二级 |园豆:208 | 2013-05-26 21:17
其他回答(1)
0

varchar2 会把 NULL 也当成 '' 只要判断 是否为空就可以。

geass.. | 园豆:1821 (小虾三级) | 2013-05-25 07:26
select case
         when '' is null then
          ''
         else
          '不等 '
       end
  from dual

select case
         when null is null then
          ''
         else
          '不等 '
       end
  from dual

--都会输出等于  :  为什么?  难到空字符串也当成null了么?  
select case 
         when null = null then
          ''
         else
          '不等 '
       end
  from dua

select case 
         when '' = '' then
          ''
         else
          '不等 '
       end
  from duall

--这两个都输出“不等” ;  为什么?  难道和case用法有关么?  求原理!

求解释!

支持(0) 反对(0) imefong | 园豆:208 (菜鸟二级) | 2013-05-25 10:16

@imefong: 之前的回复有误,Oracle 中,空字符串存入到Oracle中会自动转换为NULL,另外VARCHAR2把空串等同于null处理。

SQL> select 1 from dual where null=null;  没有查到记录  
SQL> select 1 from dual where null='';  没有查到记录  
SQL> select 1 from dual where ''='';  没有查到记录  
SQL> select 1 from dual where null is null; 

支持(0) 反对(0) geass.. | 园豆:1821 (小虾三级) | 2013-05-25 14:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册