oracle pl/sql之sql中的case语句

Oracle的case语句有两种写法

 

第一种:

     case <selector>

          when <条件表达式1> then    pl/sql语句1;

         when <条件表达式2> then    pl/sql语句2;

         ......

         when <条件表达式n> then    pl/sql语句n;

        [else pl/sql语句n+1;]

    end;

 

第二种:

case 

 

when <条件表达式1> then    pl/sql语句1;

when <条件表达式2> then    pl/sql语句2;

......

when <条件表达式n> then    pl/sql语句n;

[else pl/sql语句n+1;]

end;

 

程序演示:

set serveroutput on;
declare
begin
for i in 0..3 loop
case i
when 0 then
dbms_output.put_line('i='||'0');
when 1 then
dbms_output.put_line('i='||'1');
when 2 then
dbms_output.put_line('i='||'2');
when 3 then
dbms_output.put_line('i='||'3');
else
dbms_output.put_line('错误');
end case;
end loop;
end;

 

注意:

     注意else语句不能省略如果省略当没有与选择器匹配的when语句是就会报一个oracle错误

 

posted @ 2015-10-22 14:38  逍遥鸣  阅读(1553)  评论(0编辑  收藏  举报