为什么CASE会评估ELSE子句?

时间:2013-01-17 19:06:22

标签: oracle null oracle11g case

Oracle 11g:考虑以下两个CASE语句

搜索案例

with null_table as (select null as null_set from dual )
select 
     case 
     when (null_set is null) then 'NULL INDEED'
     else 'NOTNULL?'
     end as AM_I_NULL_OR_NOT   
from null_table

简单案例

with null_table as (select null as null_set from dual )
select 
     case null_set
     when  (null) then 'NULL INDEED'
     else 'NOTNULL?'
     end as AM_I_NULL_OR_NOT   
from null_table

Searched CASE按预期评估 null_set ,但Simple CASE似乎没有这样做。

问题: 如何对 null_set

执行简单CASE评估

为什么Simple CASE会像它一样进行评估?

1 个答案:

答案 0 :(得分:3)

在Oracle中,无法将null与使用=运算符进行比较。

例如:

select * from dual where null = null; -- No result
select * from dual where 1 = 1; -- gives a result

所以你可以做的是使用NVL将null替换为另一个值:

with null_table as (select null as null_set from dual )
select 
     case nvl(null_set,'X')
     when  'X' then 'NULL INDEED'
     else 'NOTNULL?'
     end as AM_I_NULL_OR_NOT   
from null_table

(确保您的字段永远不能等于示例中的X值。)