ITPub博客

首页 > 数据库 > 数据库开发技术 > PLSQL学习-【6异常】

PLSQL学习-【6异常】

原创 数据库开发技术 作者:哎呀我的天呐 时间:2014-11-23 18:39:44 0 删除 编辑

 java中异常处理后还可以回语句块中,但是plsql不能,这感觉挺不好的。

15:31:19 SQL> r
  1  declare
  2  v_ename emp.ename%type;
  3 begin
  4 select ename into v_ename from emp where deptno = 10;
  5  dbms_output.put_line(v_ename);
  6* end;
declare
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 4


这里出现错误程序就不能继续往下执行,我们要处理异常的


declare
v_ename emp.ename%type;
begin
 select ename into v_ename from emp where deptno = 55;
 dbms_output.put_line(v_ename);
 exception 
   when too_many_rows then
   dbms_output.put_line('return too many rows');
   when no_data_found then
   dbms_outpORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
-2292
ut.put_line('no found');
end;







这些都是有名字的异常,还有些没有名字的异常。但是有编号。

没名字有号的异常:(oracle定义的异常)


declare
  e_dept exception;
  pragma exception_init(e_dept ,-2292);
begin
  delete from dept where deptno = 10;
  commit;
  exception
  when e_dept then
  dbms_output.put_line(\'you can not delete this row\');
  rollback;
end;


有约束不能删除,e_dept是我们定义的异常的名字,我们如何确定异常号的呢?

declare
begin
   delete from dept where deptno = 10;
   exception 
    when no_data_found then 
      dbms_output.put_line('not found');
    when others then
      dbms_output.put_line(sqlerrm);
      dbms_output.put_line(sqlcode);
end;
ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
-2292


自定义异常:(非oracle异常)





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

请登录后发表评论 登录
全部评论
https://cloud.tencent.com/developer/inventory/14362

注册时间:2014-10-30

  • 博文量
    403
  • 访问量
    2528690