Oracle PL/SQL中異常高級特性示例解析

本文只介紹3種PL/SQL異常的三種高級形態,用於解決Oracle內置異常過少,很多時候不能夠滿足實際的使用需求。對oracle 高級特性相關知識感興趣的朋友一起看看吧

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_number 是與特定錯誤消息關聯的錯誤編號,Oracle預留了-20999 -- -20000專門提供給程序員自定義錯誤代碼。
  - error_message 是錯誤消息文本,最多包含2048個字符。
  - keep_errors 是可選的Boolean參數,默認爲FALSE,如果爲TRUE,新拋出的錯誤會被添加到已拋出的錯誤列表中,這個錯誤列表稱爲錯誤棧,如果爲FALSE,新錯誤會替換已拋出的錯誤棧。
 - 適用於未命名的用戶定義異常,負責把錯誤編號和錯誤消息關聯,用戶定義了異常,卻沒有定義該錯誤的名稱
 - 使用RAISE_APPLICATION_ERROR過程,程序員能夠遵循與Oracle一致的方式返回錯誤消息。

 - 示例代碼

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, if you want delete 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,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); 
 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中異常高級特性示例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章