1. 程式人生 > >《Oracle PL/SQL例項精講》學習筆記25——Oracle提供的包 (第五部分——使用DBMS_UTILITY報告錯誤)

《Oracle PL/SQL例項精講》學習筆記25——Oracle提供的包 (第五部分——使用DBMS_UTILITY報告錯誤)

本章內容:

1. 利用Oracle提供的包擴充套件功能(使用UTL_FILE訪問檔案、使用DBMS_JOB排程作業、DBMS_XPLAN生成解釋計劃、DBMS_SQL產生隱式語句結果 )

2. 利用Oracle提供的包報告錯誤(使用DBMS_UTILITY包報告錯誤、使用UTL_CALL_STACK包報告錯誤)

 

程式碼如下:

1. FORMAT_CALL_STACK (格式化輸出執行呼叫棧)

SQL> -- For Example  ch24_7.sql
SQL> CREATE OR REPLACE PROCEDURE first
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.PUT_LINE (DBMS_UTILITY.FORMAT_CALL_STACK);
  5  END first;
  6  /

Procedure created.

SQL>
SQL> CREATE OR REPLACE PROCEDURE second
  2  IS
  3  BEGIN
  4     first;
  5  END second;
  6  /

Procedure created.

SQL>
SQL> CREATE OR REPLACE PROCEDURE third
  2  IS
  3  BEGIN
  4     second;
  5  END third;
  6  /

Procedure created.

SQL>
SQL> BEGIN
  2     third;
  3  END;
  4    /
----- PL/SQL Call Stack -----
  object      line  object
  handle    number  name
00007FFB58E9FE00         4  procedure C##STUDENT.FIRST
00007FFB583C2CE8         4  procedure C##STUDENT.SECOND
00007FFB5E673BD8         4  procedure C##STUDENT.THIRD
00007FFB59EBDFB8         2  anonymous block

2. FORMAT_ERROR_BACKTRACE (格式化輸出與當前錯誤相關聯的錯誤回溯路徑)

SQL> -- For Example ch24_8.sql
SQL> CREATE OR REPLACE PROCEDURE first
  2  IS
  3     v_name VARCHAR2(30);
  4  BEGIN
  5     DBMS_OUTPUT.PUT_LINE ('procedure FIRST');
  6
  7     SELECT RTRIM(first_name)||' '||RTRIM(last_name)
  8       INTO v_name
  9       FROM student
 10      WHERE student_id = 1000;
 11  END first;
 12  /

Procedure created.

SQL>
SQL> CREATE OR REPLACE PROCEDURE second
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.PUT_LINE ('procedure SECOND');
  5     first;
  6  END second;
  7  /

Procedure created.

SQL>
SQL> CREATE OR REPLACE PROCEDURE third
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.PUT_LINE ('procedure THIRD');
  5     second;
  6  END third;
  7  /

Procedure created.

SQL> BEGIN
  2     third;
  3  EXCEPTION
  4     WHEN OTHERS
  5     THEN
  6        DBMS_OUTPUT.PUT_LINE (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
  7  END;
  8  /
procedure THIRD
procedure SECOND
procedure FIRST
ORA-06512: at "C##STUDENT.FIRST", line 7
ORA-06512: at "C##STUDENT.SECOND", line 5
ORA-06512: at "C##STUDENT.THIRD", line 5
ORA-06512: at line 2

3. FORMAT_ERROR_STACK (返回與異常相關聯的錯誤編號和錯誤資訊)

SQL> CREATE OR REPLACE PROCEDURE second
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.PUT_LINE ('procedure SECOND');
  5     first;
  6  END second;
  7  /

Procedure created.

SQL>
SQL> CREATE OR REPLACE PROCEDURE third
  2  IS
  3  BEGIN
  4     DBMS_OUTPUT.PUT_LINE ('procedure THIRD');
  5     second;
  6  END third;
  7  /

Procedure created.

SQL>
SQL> BEGIN
  2     third;
  3  EXCEPTION
  4     WHEN OTHERS
  5     THEN
  6        DBMS_OUTPUT.PUT_LINE ('Error Backtrace:');
  7        DBMS_OUTPUT.PUT_LINE ('----------------');
  8        DBMS_OUTPUT.PUT_LINE (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
  9
 10        DBMS_OUTPUT.PUT_LINE ('Error Stack:');
 11        DBMS_OUTPUT.PUT_LINE ('----------------');
 12        DBMS_OUTPUT.PUT_LINE (DBMS_UTILITY.FORMAT_ERROR_STACK);
 13  END;
 14  /
procedure THIRD
procedure SECOND
procedure FIRST
Error Backtrace:
----------------
ORA-06512: at "C##STUDENT.FIRST", line 7
ORA-06512: at "C##STUDENT.SECOND", line 5
ORA-06512: at "C##STUDENT.THIRD", line 5
ORA-06512: at line 2


Error Stack:
----------------
ORA-01403: no data found