資料庫之 異常處理篇

2021-06-05 16:49:40 字數 2270 閱讀 1467

****************************

資料庫之【異常處理篇】

****************************

--預定義異常詳細列表

access_into_null 在未初化物件時出現

case_not_found 在case語句中的選項與使用者輸入的資料不匹配時出現

collection_is_null 在給尚未初始化的表或陣列賦值時出現

cursor_already_open 在使用者試圖開啟已經開啟的游標時出現

dup_val_on_index 在使用者試圖將重複的值存在使用唯一索引的資料庫列中時出現

invalid_cursor 在執行非法游標運算(如開啟乙個尚未開啟的游標)時出現

invalid_number 在將字串轉換為數字時出現

login_denied 在輸入的使用者名稱或密碼無效時出現

no_data_found 在表中不存在的請求的行時出現,此外,當程式引用已經刪除的元素時

storage_error 在記憶體損壞或pl/sql耗盡記憶體時出現

too_many_rows 在執行select into語句後返回多行時出現

value_error 在產生大小限制錯誤時出現

zero_divide 以零作除數時出現

others 所有異常

-在plsql塊中,如果不使用錯誤捕獲,可以直接使用

--錯誤處理,-20001/*-20000到20999*/, '未指定項費率'/*2048位元組*/,在語句執行部分使用,如果不是自定義錯誤的丟擲,則不能與exception一起使用

--如果要列印出所有錯誤資訊:

dbms_output.put_line(sqlcode||sqlerrm);

--sqlcode錯誤編號

--sqlerrm錯誤資訊

declare

--預定義錯誤(返回多行)

s_al number;

begin

select sal into s_al from emp;

exception

when too_many_rows then

dbms_output.put_line('返回多行');

end;

declare

--記錄沒找到

s_al number;

begin

select sal into s_al from emp where empno = 1;

exception

when no_data_found then

dbms_output.put_line('記錄沒找到');

end;

declare

--自定義錯誤

s_al number;

err exception;

begin

select sal into s_al from emp where empno = 7369;

if s_al < 3000 then

dbms_output.put_line(s_al);

raise err;

else

dbms_output.put_line(s_al);

end if;

exception

when err then

dbms_output.put_line('工資太低');

end;

--引發應用程式錯誤

declare

s_al number;

err exception; --定義錯誤

begin

select sal into s_al from emp where empno = 7369;

if s_al < 3000 then

dbms_output.put_line(s_al);

raise err; --丟擲錯誤

else

dbms_output.put_line(s_al);

end if;

exception

--錯誤捕獲

when err then

end;

資料庫 異常處理

處理步驟 declare 宣告變數 begin 處理邏輯 exception 處理異常 when 異常1 then when 異常2 then when others then 處理其他異常 end 常見異常 zero divide 除零異常 value error 型別轉換異常 too many ...

Oracle資料庫之PL SQL異常處理

異常指的是在程式執行過程中發生的異常事件,通常是由硬體問題或者程式設計問題所導致的。pl sql程式設計過程中,即使是寫得最好的程式也可能會遇到錯誤或未預料到的事件。乙個健壯的程式都應該能夠正確處理各種異常情況,並盡可能從中恢復。1.異常處理 異常處理是用來處理正常執行過程中未預料的事件。pl sq...

Oracle資料庫 異常處理

oracle異常處理 在pl sql語句書寫時,需要處理的異常 不做異常處理時 declare v name emp.ename type v sal emp.sal type begin select ename,sal into v name,v sal from emp where empno...