ITPub博客

首页 > 数据库 > Oracle > 【ORACLE】pl/sql编程例外处理(Exception) 李四

【ORACLE】pl/sql编程例外处理(Exception) 李四

Oracle 作者:xiaojianzhong 时间:2014-01-10 19:31:33 0 删除 编辑

例外分为三种:1.预定义例外 2.非预定义例外 2.自定义例外

以下主要举例说明预定义例外处理

1.预定义例外 no_data_found.

declare

v_ename emp ename%type

begin

select ename into v_ename from emp where empno=&gno;

dbms_output.put_line('名字:'||v_ename);

exception

   when no_data_found then

   dbms_output.put_line('该编号没有!');

end;

 

2.预定义例外 case_no_found.

在pl/sql块中编辑case语句时,如果when 子句中没有包含必须的条件分支,就会触发case_no_found.

create or replace procedure xm_pro1(xmno  number) is

  v_sal emp.sal%type;

begin

select sal into v_sal from emp where empno=xmno;

case

when v_sal<5000 then

update emp set sal=sal+1000 where empno=xmno;

when v_sal<8000 then

update emp set sal=sal+2000 where empno=xmno;

end case;

exception

when case_no_found then

dbms_output.put_line('case语句中没有与'||v_sal||'相匹配的条件');

end;

 

3.预定义例外 dup_val_on_index

在唯一索引所对应的列上插入重复值时,会隐含的触发例外

begin

insert into dept values(10,'财务部','广州');

exception

when dup_val_on_index then

dbms_output.put_line('在deptno 列上不能出现重复值');

end;

 

4.预定义例外 cursor_already_open.

当重复打开游标时,会触发例外

declare

 cursor emp_cursor is select ename,sal from emp;

begin

open emp_cursor;

for emp_record1 in emp_cursor loop;

dbms-output.put_line(emp_record1.ename);

end loop;

exception

when cursor_already_open then

dbms_output.put_line('游标已经打开');

end;

 

其他预定义例外用法大同小异,就不在多说了。

5. login_denide  当用户非法登录时,触发此例外

6. not_logged_on  当用户没有登录就执行dml操作时,就会触发此例外

7. storage_error  如果超出内存空间或是内存被损坏,触发此例外

8. timeout_on_resource  如果oracle在等待资源时,出现了超时就会触发此例外。

...

...

 

<!-- 正文结束 -->

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

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-02-27