plsql_case when_end case学习小例

SQL> r
  1  declare
  2  i integer:=0;
  3  begin
  4  case
  5   when(i<0) then i:=1;
  6   when(i>1 and i<10) then i:=8;
  7  end case;
  8  dbms_output.put_line(i);
  9* end;
declare
*
ERROR at line 1:
ORA-06592: CASE not found while executing CASE statement  ---出现这个oracle错误,说明 case条件一个也不符合,下列的plsql可以抓取这个异常case_not_found
ORA-06512: at line 4

 

 

SQL> r
  1  declare
  2  i integer:=0;
  3  begin
  4  case
  5   when(i<0) then i:=1;
  6   when(i>1 and i<10) then i:=8;
  7  end case;
  8  dbms_output.put_line(i);---正常情况,打印这个变量i的值
  9  exception  --exception语法 exception when 异常名字 then 动作;
 10    when case_not_found then
 11    dbms_output.put_line(i); --抓取这个异常,并打印这个变量i值
 12* end;
0

PL/SQL procedure successfully completed.

SQL>

 

---在plsql中(声明部分定义一个过程或者函数,并在plsql主体内调用声明部分定义的过程或函数);这是另一种plsql 中case when end case的语法
SQL> r
  1  declare
  2  dname test.dname%type;
  3  procedure p_print(s varchar2) is
  4  begin
  5   dbms_output.put_line(s);
  6  end;
  7  begin
  8  dname:='it';
  9  case dname  --变量名称
 10   when 'worker' then p_print('worker');  --when后跟变量的值,注意这是一个比较值
 11   when 'it' then p_print('it');
 12   when 'mark' then p_print('mark');
 13   else   p_print('none');--若以上when皆不满足,采用else的动作
 14  end case;
 15* end;
it

PL/SQL procedure successfully completed.

请使用浏览器的分享功能分享到微信等