Oracle PL/SQL中EXCEPTION用法
1.自定義EXCEPTION
DECLARE
past_due EXCEPTION;
acct_num NUMBER := 2;
BEGIN
DECLARE ---------- sub-block begins
past_due EXCEPTION; -- this declaration prevails
acct_num NUMBER :=3;
due_date DATE := SYSDATE - 1;
todays_date DATE := SYSDATE;
BEGIN
IF due_date < todays_date THEN
RAISE past_due; -- this is not handled
END IF;
EXCEPTION
WHEN past_due THEN -- does not handle raised EXCEPTION
DBMS_OUTPUT.PUT_LINE('Handling PAST_DUE exception.'||acct_num);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Could not recognize PAST_DUE_EXCEPTION in this scope.'||acct_num);
END; ------------- sub-block ends
EXCEPTION
WHEN past_due THEN -- does not handle raised exception
DBMS_OUTPUT.PUT_LINE('Handling PAST_DUE exception.'||acct_num);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Could not recognize PAST_DUE_EXCEPTION in this scope.'||acct_num);
END;
DECLARE
out_of_stock EXCEPTION;
number_on_hand NUMBER := 0;
BEGIN
IF number_on_hand < 1 THEN
RAISE out_of_stock; -- raise an exception that we defined
END IF;
EXCEPTION
WHEN out_of_stock THEN
-- handle the error
DBMS_OUTPUT.PUT_LINE('Encountered out-of-stock error.');
END;
2. 使用 oracle 自帶的 error 返回
DECLARE
acct_type INTEGER := 7;
BEGIN
IF acct_type NOT IN (1, 2, 3) THEN
RAISE INVALID_NUMBER; -- raise predefined exception
END IF;
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('HANDLING INVALID INPUT BY ROLLING BACK.');
ROLLBACK;
END;
3. Retrieving the Error Code and Error Message: SQLCODE and SQLERRM
CREATE TABLE errors (code NUMBER, message VARCHAR2(64), happened TIMESTAMP);
DECLARE
names employee.name%TYPE;
v_code NUMBER;
v_errm VARCHAR2(64);
BEGIN
SELECT name INTO names FROM employee WHERE id = -1;
EXCEPTION
WHEN OTHERS THEN
v_code := SQLCODE;
v_errm := SUBSTR(SQLERRM, 1 , 64);
DBMS_OUTPUT.PUT_LINE('Error code ' || v_code || ': ' || v_errm);
-- Normally we would call another procedure, declared with PRAGMA
-- AUTONOMOUS_TRANSACTION, to insert information about errors.
INSERT INTO errors VALUES (v_code, v_errm, SYSTIMESTAMP);
commit;
END;
4. raise_application_error
DECLARE
num_tables NUMBER;
BEGIN
SELECT COUNT(*) INTO num_tables FROM USER_TABLES;
IF num_tables < 1000 THEN
/* Issue your own error code (ORA-20101) with your own error message.
Note that you do not need to qualify raise_application_error with
DBMS_STANDARD */
raise_application_error(-20101, 'Expecting at least 1000 tables');
ELSE
NULL; -- Do the rest of the processing (for the non-error case).
END IF;
END;
5.指定PRAGMA EXCEPTION_INIT(COMPILATION_ERROR,ERROR_CODE)
CREATE OR REPLACE PROCEDURE SFIS1.execute_immediate( p_sql_text VARCHAR2 ) IS
COMPILATION_ERROR EXCEPTION;
PRAGMA EXCEPTION_INIT(COMPILATION_ERROR,-24344);
l_cursor INTEGER DEFAULT 0;
rc INTEGER DEFAULT 0;
stmt VARCHAR2(1000);
BEGIN
l_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(l_cursor, p_sql_text, DBMS_SQL.NATIVE);
rc := DBMS_SQL.EXECUTE(l_cursor);
DBMS_SQL.CLOSE_CURSOR(l_cursor);
--
-- Ignore compilation errors because these sometimes happen due to
-- dependencies between views AND procedures
--
EXCEPTION
WHEN COMPILATION_ERROR THEN
DBMS_SQL.CLOSE_CURSOR(l_cursor);
WHEN OTHERS THEN
BEGIN
DBMS_SQL.CLOSE_CURSOR(l_cursor);
raise_application_error(-20101,sqlerrm || ' when executing ''' || p_sql_text || ''' ');
END;
END;
CREATE UNIQUE INDEX GC.EMP_NO_ ON GC.EMP(EMP_NAME)
DECLARE
EMP_NAME_UNIQUE EXCEPTION;
PRAGMA EXCEPTION_INIT(EMP_NAME_UNIQUE, -00001);
BEGIN
INSERT INTO GC.EMP SELECT * FROM GC.EMP;
EXCEPTION
WHEN EMP_NAME_UNIQUE THEN
DBMS_OUTPUT.PUT_LINE('違反一致性');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE||'---'||SQLERRM);
END;
6.DBMS_UTILITY.format_error_backtrace,DBMS_UTILITY.format_error_stack 返回錯誤行和錯誤
DECLARE
V_TABLE_NAME VARCHAR2 (500);
BEGIN
SELECT TABLE_NAME INTO V_TABLE_NAME FROM DBA_TABLES;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_backtrace|| '--'|| DBMS_UTILITY.format_error_stack);
--DBMS_OUTPUT.put_line ('error line:' || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE () || ' SQLCODE:'|| SQLCODE|| ' SQLERRM:'|| SQLERRM);
END;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25583515/viewspace-2147280/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle PL/SQLOracleSQL
- Oracle PL/SQL程式碼中的註釋OracleSQL
- Oracle PL/SQL塊簡介OracleSQL
- 【OracleEBS】 在PL/SQL中呼叫Oracle ERP請求OracleSQL
- Oracle 的PL/SQL語言使用OracleSQL
- 使用profiler測試Oracle PL/SQL效能OracleSQL
- Oracle 中的exception——(Raise)OracleExceptionAI
- PL/SQL Developer連線到Oracle 12cSQLDeveloperOracle
- 「Oracle」客戶端 PL/SQL DEVELOPER 安裝使用Oracle客戶端SQLDeveloper
- Oracle exceptionOracleException
- Oracle vs PostgreSQL Develop(23) - PL(pg)sql(引數宣告)OracleSQLdev
- 原創:oracle PL/SQL程式設計基礎 上OracleSQL程式設計
- 原創:oracle PL/SQL程式設計基礎 下OracleSQL程式設計
- PL/SQL Developer連線遠端Oracle資料庫SQLDeveloperOracle資料庫
- PL/SQL 宣告SQL
- 【TUNE_ORACLE】列出LOOP套LOOP的PL/SQL程式碼SQL參考OracleOOPSQL
- [20240607]PL/SQL中sql語句的註解.txtSQL
- 5. Oracle連線和使用——5.2. PL/SQL DeveloperOracleSQLDeveloper
- oracle學習筆記(十七) PL/SQL高階應用Oracle筆記SQL
- pl/sql to_dateSQL
- PL/SQL 運算子SQL
- PL/SQL 條件SQL
- PL/SQL 迴圈SQL
- PL/SQL 遊標SQL
- 【SQL】Oracle查詢轉換之 OR用法SQLOracle
- Oracle 19c Concepts(08):Server-Side Programming: PL/SQL and JavaOracleServerIDESQLJava
- 本地不安裝oracle,用PL/SQL Developer連線資料庫OracleSQLDeveloper資料庫
- 如何在PL/SQL中讀寫檔案(轉)SQL
- oracle學習筆記(十五) PL/SQL語法結構以及使用Oracle筆記SQL
- SQL中Merge的用法SQL
- oracle中substr() instr() 用法Oracle
- oracle 使用異常exceptionOracleException
- PL/SQL中動態掉用儲存過程SQL儲存過程
- ultraedit高亮顯示pl/sqlSQL
- SQL中的遞迴用法SQL遞迴
- oracle樹中prior的用法Oracle
- 使用PL/SQL找到兩個表中的相似值FKSQL
- PL/SQL 條件控制語句SQL
- PL/SQL程式設計急速上手SQL程式設計