ITPub博客

首页 > Linux操作系统 > Linux操作系统 > plsql_case when_end case学习小例

plsql_case when_end case学习小例

原创 Linux操作系统 作者:wisdomone1 时间:2010-08-17 18:59:37 0 删除 编辑

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.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9240380/viewspace-671198/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
提供针对oracle初学者及进阶的数据库培训,欢迎大家咨询: 微信: wisdomone 微信公众号: lovedb 工作经历: 中国普天 北京科蓝 北京云和恩墨 北京神州新桥

注册时间:2008-04-04

  • 博文量
    2189
  • 访问量
    12689906