PLSQL學習-【6異常】

哎呀我的天吶發表於2014-11-23

 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部落格 ” ,連結:http://blog.itpub.net/29990276/viewspace-1343627/,如需轉載,請註明出處,否則將追究法律責任。

相關文章