濮阳杆衣贸易有限公司

主頁 > 知識庫 > Oracle PL/SQL中異常高級特性示例解析

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

熱門標簽:圖像地圖標注 貴陽電話外呼系統(tǒng)哪家好 呼倫貝爾智能手機地圖標注 安陽外呼系統(tǒng)免費 分布式呼叫中心 400電話是不是免費申請 濟南地圖標注公司 南寧人工智能電銷機器人費用 海南400電話哪里辦理

PL/SQL(Procedural Language/SQL,過程語言/SQL)是結(jié)合了Oracel過程語言和結(jié)構(gòu)化查詢語言(SQL)的一種擴展語言。

優(yōu)點:

(1)PL/SQL具有編程語言的特點,它能把一組SQL語句放到一個模塊中,使其更具模塊化種序的特點。

(2)PL/SQL可以采用過程性語言控制程序的結(jié)構(gòu)。

(3)PL/SQL有自動處理的異常處理機制。

(4)PL/SQL程序塊具有更好的可移植性,可移植到另一個Oracle數(shù)據(jù)庫中。

(5)PL/SQL程序減少了網(wǎng)絡的交互,有助于提高程序性能。

在OraclePL/SQL語句塊中exception的異常處理部分是非常重要的組成部分,它決定了在PL/SQL語句塊內(nèi)部可執(zhí)行部分在發(fā)生異常錯誤時,程序是友好地提示:程序遇到某些錯誤而無法執(zhí)行,還是拋出一堆難以理解的Oracle內(nèi)部錯誤碼。

  本文只介紹3種PL/SQL異常的三種高級形態(tài),用于解決Oracle內(nèi)置異常過少,很多時候不能夠滿足實際的使用需求。

1,RAISE_APPLICATION_ERROR

 - 是Oracle提供的一種特殊的內(nèi)置過程,允許程序員為特定的程序創(chuàng)建有意義的錯誤消息,適用于用戶自定義定義異常。
 - 語法結(jié)構(gòu)
  RAISE_APPLICATION_ERROR (error_number,error_message);或者
  RAISE_APPLICATION_ERROR (error_number,error_message,keep_errors)
  - error_number 是與特定錯誤消息關聯(lián)的錯誤編號,Oracle預留了-20999 -- -20000專門提供給程序員自定義錯誤代碼。
  - error_message 是錯誤消息文本,最多包含2048個字符。
  - keep_errors 是可選的Boolean參數(shù),默認為FALSE,如果為TRUE,新拋出的錯誤會被添加到已拋出的錯誤列表中,這個錯誤列表稱為錯誤棧,如果為FALSE,新錯誤會替換已拋出的錯誤棧。
 - 適用于未命名的用戶定義異常,負責把錯誤編號和錯誤消息關聯(lián),用戶定義了異常,卻沒有定義該錯誤的名稱
 - 使用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代碼會根據(jù)用戶輸入的員工Id,查詢員工的姓名和工資。當我們輸入存在的員工編號時,程序能夠正常返回結(jié)果;如果輸入不存在ID,則select into語句會拋出沒有返回行,進而使程序進入異常處理部分(本部分為舉例),程序同樣執(zhí)行成功;當輸入一個負數(shù)時,if條件語句就會進入到raise_application_error部分,由于可執(zhí)行部分運行發(fā)生錯誤,執(zhí)行焦點會立即轉(zhuǎn)移到異常處理部分,而異常處理部分沒有關于該異常的處理,所以程序報錯,并返回到用戶界面。

 - 是喲個raise_application_error,程序員可以使程序?qū)崿F(xiàn)像Oracle系統(tǒng)產(chǎn)生的錯誤消息。

 - 事實上,單純使用raise_application_error,因為沒有異常的名稱,如果要對其進行異常處理,只能夠使用others(下文有專門的介紹)。

2,EXCEPTION_INIT

 - 使用EXCEPTION_INIT編譯指令,可以將用戶自定義的Oracle錯誤編號和用戶自定義的錯誤名稱關聯(lián)起來,相當于用戶自定義錯誤和RAISE_APPLICATION_ERROR的結(jié)合體。

 - EXCEPTION_INIT 出現(xiàn)在語句塊的聲明部分: 

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 進行關聯(lián)。當程序執(zhí)行報錯時進入異常處理部分,在這里我們重新給這個錯誤定義了錯誤消息。

3,SQLCODE 和 SQLERRM

 - 在異常處理中,當異常的名稱未知時(比如上面1中RAISE_APPLICATION_ERROR),都可以使用others來進行異常的捕獲處理;

 - 由于others所捕獲的異常是未知的(也可以是已知的,但是在程序中沒有將其枚舉出來),因此需要使用Oracle提供的兩個內(nèi)置函數(shù)SQLCODE、SQLERRM來針對others的異常進行處理:

 - SQLCODE 會返回Oracle的錯誤編號
 - SQLERRM,返回錯誤的消息

 - 示例1,處理Oracle系統(tǒng)返回的錯誤:

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以字面值賦值的方法給這兩個變量。

總結(jié)

以上所述是小編給大家介紹的Oracle PL/SQL中異常高級特性示例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • Oracle基本PLSQL的使用實例詳解
  • 詳解PL/SQL Developer連接本地Oracle 11g 64位數(shù)據(jù)庫
  • win7 64位操作系統(tǒng)中Oracle 11g + plsql安裝教程詳解(圖解)
  • Plsql Developer連接Oracle時出現(xiàn)Could not initialize oci.dll解決方案
  • 使用PL/SQL Developer連接Oracle數(shù)據(jù)庫的方法圖解
  • Windows 64位下裝安裝Oracle 11g,PLSQL Developer的配置問題,數(shù)據(jù)庫顯示空白的完美解決方案(圖文教程)
  • PL/SQL遠程備份和恢復Oracle數(shù)據(jù)庫
  • Oracle客戶端與plsql查詢數(shù)據(jù)亂碼修改成中文的快速解決方法

標簽:南充 許昌 合肥 郴州 滁州 涼山 焦作 遼源

巨人網(wǎng)絡通訊聲明:本文標題《Oracle PL/SQL中異常高級特性示例解析》,本文關鍵詞  Oracle,SQL,中,異常,高級,特性,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 下面列出與本文章《Oracle PL/SQL中異常高級特性示例解析》相關的同類信息!
  • 本頁收集關于Oracle PL/SQL中異常高級特性示例解析的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    海原县| 天长市| 固阳县| 沿河| 安化县| 香河县| 清镇市| 临江市| 泰和县| 高邑县| 丹江口市| 新巴尔虎左旗| 古交市| 重庆市| 阿合奇县| 德保县| 晴隆县| 渭南市| 庄浪县| 原平市| 漯河市| 历史| 吴川市| 红河县| 加查县| 浏阳市| 涟水县| 馆陶县| 剑川县| 禹州市| 东安县| 合作市| 舒城县| 湘乡市| 和平县| 隆德县| 瑞丽市| 德州市| 贵定县| 磴口县| 新乐市|