Oracle pl/sql编程 10---case语句

来源:互联网 发布:windows10 数据丢失 编辑:程序博客网 时间:2024/05/21 15:00

Oracle的case语句有两种写法

第一种

case <selector>

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

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

......

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

[elsepl/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;   begin    for i in 0..5 LOOP    CASE i    when 0 then      dbms_output.put_line('i is zero');         when 1 then      dbms_output.put_line('i is 1');          when 2 then      dbms_output.put_line('i is 2');          when 3 then      dbms_output.put_line('i is 3');          when 4 then      dbms_output.put_line('i is 4');       else      dbms_output.put_line('i is 5');     end case;     end loop;            end;

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

begin    for i in 0..5 LOOP    CASE i    when 0 then      dbms_output.put_line('i is zero');         when 1 then      dbms_output.put_line('i is 1');          when 2 then      dbms_output.put_line('i is 2');          when 3 then      dbms_output.put_line('i is 3');          when 4 then      dbms_output.put_line('i is 4');     end case;     end loop;   end;ORA-06592: 执行 CASE 语句时未找到 CASEORA-06512: 在 line 3


0 0