case语句跟case表达式区别

本文详细解析了PL/SQL中CASE语句的使用方法及注意事项,包括CASE语句与CASE表达式的区别,如何避免因缺少ELSE子句而导致的ORA-06592错误,以及CASE表达式返回NULL的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CASE语句,结尾要加end case,碰到没有处理的分支而又缺少ELSE子句就会报错:"ORA-06592: CASE not found while executing CASE statement"

declare
str varchar2(50);
v   varchar2(50):='scott';
begin
   case v   --没有匹配的case会报异常,可以在最后写上else
   when 'scotst' then
   str := 's';
   else
    str:='z';
   end case;  --case不能省略
   dbms_output.put_line(str);
end;

CASE表达式,结尾不能加end case,在没有匹配的选项又没有ELSE的情况下返回NULL。

declare
  str varchar2(50);
  v   varchar2(50) := 'scott';
begin
  str := case v --没有匹配的case会返回null
    when 'scotst' then 's' end;  --不能加 end case
  dbms_output.put_line(str);  --null
end;
select * from dual where 1 = case 1 when 1 then 1 end; --不能加end case
select case rownum
         when 1 then
          100
       end case --case可有可无,作列名处理
  from dual
connect by rownum < 10
select count(*) from dual where case 1 when 2 then 1 end is null; --1,没有匹配的值时返回null



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值