ORACLE PLSQL 異常處理

2021-08-11 13:32:49 字數 1832 閱讀 8864

例一

create table testerr(id number(10),name varchar2(10));

insert into testerr values(1,'test');

insert into testerr values(2,'test');

insert into testerr values(3,'test1');

--no_data_found和too_many_rows是經常發生的異常,所以對於plsql寫這樣的異常捕獲是很好的

declare

a testerr%rowtype; --define a variable

begin

--這裡的異常**在自己的塊中,發生異常不會影響其他塊,只在本塊內處理掉了。

--如果將有異常**區的塊不捕獲異常,則自動傳播到外層塊。如果外層塊處理,則不會列印1,如果繼續

--不處理,則顯示丟擲。

begin

select * into a from testerr where name='test';

--start trap exception

exception

when no_data_found then

dbms_output.put_line('no data found in table testerr!');

when too_many_rows then

dbms_output.put_line('to many rows found in this query!'||sqlcode||','||sqlerrm);

when others then

dbms_output.put_line('others error occur!');

end;

begin

dbms_output.put_line(1);

end;

end;

--結果是:

to many rows found in this query!-1422,ora-01422: 實際返回的行數超出請求的行數 例二

create table classes(id number(10) primary key,name varchar2(10));

create table classes_student(id number(10) primary key,name varchar2(10),

classes_id number(10) references classes(id));

insert into classes values(1,'s301');

insert into classes_student values(1,'dj',1);

commit;--系統非預定義異常

declare

cannot_dele exception;

--2292是違反完整性約束的錯誤

pragma exception_init(cannot_dele,-2292);

begin

delete from classes where id=1;

commit; --start exception handle

exception

when cannot_dele then

dbms_output.put_line('can''t delete classes_student!'||sqlcode||':'||sqlerrm);

end;

--can't delete classes_student!-2292:ora-02292: 違反完整約束條件 (dingjun123.sys_c0011750) - 已找到子記錄

Oracle PL SQL異常處理

case語句語法格式如下 case 變數 when 表示式1 then 值1 when 表示式2 then 值2 when 表示式n then 值n else 值n 1 end 1 使用case語句寫乙個pl sql塊,要求輸入員工編號,根據員工的職位進行工資提公升,提公升要求如下 如果職位是cle...

Oracle PL SQL異常處理

oracle的exception init編譯指令declare exception name exception pragma exception init exception name,error code 下面給乙個例項 declare v num number sv num v result...

Oracle plsql異常處理

oracle中出現錯誤的情形通常分為編譯時錯誤 compile timeerror 和執行時錯誤 run time error 異常是在pl sql執行過程中出現的警告或錯誤。異常是如何觸發的?發生了乙個 oracle 錯誤時 使用raise語句顯式觸發 如何處理異常?用處理機截獲 在呼叫環境中傳播...