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