Oracle PL/SQL中異常高階特性示例解析
PL/SQL(Procedural Language/SQL,過程語言/SQL)是結合了Oracel過程語言和結構化查詢語言(SQL)的一種擴充套件語言。
優點:
(1)PL/SQL具有程式語言的特點,它能把一組SQL語句放到一個模組中,使其更具模組化種序的特點。
(2)PL/SQL可以採用過程性語言控制程式的結構。
(3)PL/SQL有自動處理的異常處理機制。
(4)PL/SQL程式塊具有更好的可移植性,可移植到另一個Oracle資料庫中。
(5)PL/SQL程式減少了網路的互動,有助於提高程式效能。
在OraclePL/SQL語句塊中exception的異常處理部分是非常重要的組成部分,它決定了在PL/SQL語句塊內部可執行部分在發生異常錯誤時,程式是友好地提示:程式遇到某些錯誤而無法執行,還是丟擲一堆難以理解的Oracle內部錯誤碼。
本文只介紹3種PL/SQL異常的三種高階形態,用於解決Oracle內建異常過少,很多時候不能夠滿足實際的使用需求。
1,RAISE_APPLICATION_ERROR
- 是Oracle提供的一種特殊的內建過程,允許程式設計師為特定的程式建立有意義的錯誤訊息,適用於使用者自定義定義異常。
- 語法結構
RAISE_APPLICATION_ERROR (error_number,error_message);或者
RAISE_APPLICATION_ERROR (error_number,error_message,keep_errors)
- error_message 是錯誤訊息文字,最多包含2048個字元。
- keep_errors 是可選的Boolean引數,預設為FALSE,如果為TRUE,新丟擲的錯誤會被新增到已丟擲的錯誤列表中,這個錯誤列表稱為錯誤棧,如果為FALSE,新錯誤會替換已丟擲的錯誤棧。
- 適用於未命名的使用者定義異常,負責把錯誤編號和錯誤訊息關聯,使用者定義了異常,卻沒有定義該錯誤的名稱
- 示例程式碼
declare v_id number := &p_id; v_name varchar2(20); v_sal number; begin if v_id > 0 then select ename,sal into v_name,v_sal from emp where empno = v_id; dbms_output.put_line(chr(10)||v_name||' '||v_sal); else raise_application_error (-20001,'Employee id can not be negative.'); end if; exception when NO_DATA_FOUND then dbms_output.put_line(chr(10)||'There is no such employee id is '||v_id); end; / Enter value for p_id: 40 old 2: v_id number := &p_id; new 2: v_id number := 40; There is no such employee id is 40 PL/SQL procedure successfully completed. / Enter value for p_id: -90 old 2: v_id number := &p_id; new 2: v_id number := -90; declare * ERROR at line 1: ORA-20001: Employee id can not be negative. ORA-06512: at line 11
- 示例解析:該PL/SQL程式碼會根據使用者輸入的員工Id,查詢員工的姓名和工資。當我們輸入存在的員工編號時,程式能夠正常返回結果;如果輸入不存在ID,則select into語句會丟擲沒有返回行,進而使程式進入異常處理部分(本部分為舉例),程式同樣執行成功;當輸入一個負數時,if條件語句就會進入到raise_application_error部分,由於可執行部分執行發生錯誤,執行焦點會立即轉移到異常處理部分,而異常處理部分沒有關於該異常的處理,所以程式報錯,並返回到使用者介面。
- 是喲個raise_application_error,程式設計師可以使程式實現像Oracle系統產生的錯誤訊息。
- 事實上,單純使用raise_application_error,因為沒有異常的名稱,如果要對其進行異常處理,只能夠使用others(下文有專門的介紹)。
2,EXCEPTION_INIT
- 使用EXCEPTION_INIT編譯指令,可以將使用者自定義的Oracle錯誤編號和使用者自定義的錯誤名稱關聯起來,相當於使用者自定義錯誤和RAISE_APPLICATION_ERROR的結合體。
- EXCEPTION_INIT 出現在語句塊的宣告部分:
exception_name exception; pragma exception_init(exception_name,error_code)
- 考慮如下程式碼:
declare v_no number := &p_no; begin delete from dept where deptno = v_no; dbms_output.put_line(chr(10)||'The department id is '||v_no||' has been deleted'); end; / Enter value for p_no: 20 old 2: v_no number := &p_no; new 2: v_no number := 20; declare * ERROR at line 1: ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found ORA-06512: at line 4
- 由於違反外來鍵約束,刪除部門失敗了。但是丟擲的錯誤不是很好理解
- 我們可以使用EXCEPTION_INIT來對這個錯誤進行處理,首先我們得知道違反外來鍵約束的這個Oracle錯誤程式碼“ORA-02292”
- 使用EXCEPTION_INIT
declare v_no number := &p_no; e_dept_exist exception; pragma exception_init(e_dept_exist,-02292); begin delete from dept where deptno = v_no; dbms_output.put_line(chr(10)||'The department id is '||v_no||' has been deleted'); exception when e_dept_exist then dbms_output.put_line(chr(10)||'There are some employees in this deptartment,if you want delete this deptartment,please delete these employees in the department first.'); end; / Enter value for p_no: 20 old 2: v_no number := &p_no; new 2: v_no number := 20; There are some employees in this deptartment,please delete these employees in the department first. PL/SQL procedure successfully completed.
- 這下丟擲的錯誤就容易理解多了。首先我們定義了一個名為e_dept_exist的異常,然後將這個異常與Oracle錯誤程式碼 -02292 進行關聯。當程式執行報錯時進入異常處理部分,在這裡我們重新給這個錯誤定義了錯誤訊息。
3,SQLCODE 和 SQLERRM
- 在異常處理中,當異常的名稱未知時(比如上面1中RAISE_APPLICATION_ERROR),都可以使用others來進行異常的捕獲處理;
- 由於others所捕獲的異常是未知的(也可以是已知的,但是在程式中沒有將其枚舉出來),因此需要使用Oracle提供的兩個內建函式SQLCODE、SQLERRM來針對others的異常進行處理:
- SQLCODE 會返回Oracle的錯誤編號
- SQLERRM,返回錯誤的訊息
- 示例1,處理Oracle系統返回的錯誤:
declare v_no number := &p_no; error_code number; error_msg varchar2(500); begin delete from dept where deptno = v_no; dbms_output.put_line(chr(10)||'The department id is '||v_no||' has been deleted'); exception when others then error_code := sqlcode; error_msg := sqlerrm; dbms_output.put_line(chr(10)||'Error code is: '||error_code); dbms_output.put_line(chr(10)||'Error message is: '||error_msg); end; Enter value for p_no: 10 old 2: v_no number := &p_no; new 2: v_no number := 10; Error code is: -2292 Error message is: ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found PL/SQL procedure successfully completed.
- 請注意exception異常處理部分,在該部分裡面我們用到了宣告部分定義的兩個變數,error_code用來儲存SQLCODE,error_msg用來儲存SQLERRM。然後將兩個變數值打印出來。
- 示例2,處理使用者自定義的異常:
declare v_id number := &p_id; v_name varchar2(20); v_sal number; begin if v_id > 0 then select ename,'Employee id can not be negative.'); end if; exception when NO_DATA_FOUND then dbms_output.put_line(chr(10)||'There is no such employee id is '||v_id); when others then declare error_code number; error_msg varchar2(500); begin error_code := sqlcode; error_msg := sqlerrm; dbms_output.put_line(chr(10)||'Error code is: '||error_code); dbms_output.put_line(chr(10)||'Error message is: '||error_msg); end; end; / Enter value for p_id: -90 old 2: v_id number := &p_id; new 2: v_id number := -90; Error code is: -20001 Error message is: ORA-20001: Employee id can not be negative. PL/SQL procedure successfully completed.
- 在本程式碼中使用了raise_application_error,由於單純的使用raise_application_error,只能使用others進行捕獲。在異常處理部分,我們使用了一個PL/SQL語句塊來處理這個錯誤,宣告兩個變數,並將SQLCODE和SQLERRM以字面值賦值的方法給這兩個變數。
總結
以上所述是小編給大家介紹的Oracle PL/SQL中異常高階特性示例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!