oracle筆記異常處理
阿新 • • 發佈:2018-12-19
/* pl/sql異常處理 */ --預定義的異常 DECLARE V_SALARY EMPLOYEES.SALARY%TYPE; BEGIN --查詢結果會返回多條資料 SELECT SALARY INTO V_SALARY FROM EMPLOYEES WHERE EMPLOYEE_ID > 100; DBMS_OUTPUT.PUT_LINE('salary=' || V_SALARY); EXCEPTION WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('輸出的行數太多了!!'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('出現其他型別的異常了!!'); END; --非預定義的異常 DECLARE --宣告一個異常型別的變數 e_deleteEmpid_exception EXCEPTION; --讓宣告的異常變數和錯誤號關聯起來 PRAGMA EXCEPTION_INIT(e_deleteEmpid_exception, -02292); BEGIN DELETE FROM employees WHERE employee_id = 100; --執行刪除操作的時候,會報錯,錯誤號是-02292 EXCEPTION --使用異常變數 WHEN e_deleteEmpid_exception THEN dbms_output.put_line('違反完整性約束條件,因此不能刪除此使用者!!!'); END; --使用者自定義的異常處理 DECLARE e_too_high_salary EXCEPTION; v_salary employees.salary%TYPE; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 100; IF v_salary > 12000 THEN RAISE e_too_high_salary; --RAISE關鍵字,類似於java中的丟擲異常 END IF; EXCEPTION WHEN e_too_high_salary THEN dbms_output.put_line('薪水太高了!!!!'); END; --多個異常 DECLARE e_too_high_salary EXCEPTION; v_salary employees.salary%TYPE; e_deleteEmpid_exception EXCEPTION; --讓宣告的異常變數和錯誤號關聯起來 PRAGMA EXCEPTION_INIT(e_deleteEmpid_exception, -02292); BEGIN DELETE FROM employees WHERE employee_id = 100; --執行刪除操作的時候,會報錯,錯誤號是-02292 SELECT salary INTO v_salary FROM employees WHERE employee_id = 100; IF v_salary > 12000 THEN RAISE e_too_high_salary; --RAISE關鍵字 END IF; EXCEPTION WHEN e_too_high_salary THEN dbms_output.put_line('薪水太高了!!!!'); WHEN e_deleteEmpid_exception THEN dbms_output.put_line('違反完整性約束條件,因此不能刪除此使用者!!!'); WHEN OTHERS THEN dbms_output.put_line('發生其他型別的異常了!!!'); END; --多個異常 DECLARE e_too_high_salary EXCEPTION; v_salary employees.salary%TYPE; e_deleteEmpid_exception EXCEPTION; --讓宣告的異常變數和錯誤號關聯起來 PRAGMA EXCEPTION_INIT(e_deleteEmpid_exception, -02292); BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 100; IF v_salary > 12000 THEN RAISE e_too_high_salary; --RAISE關鍵字 END IF; DELETE FROM employees WHERE employee_id = 100; --執行刪除操作的時候,會報錯,錯誤號是-02292 EXCEPTION WHEN e_too_high_salary THEN dbms_output.put_line('薪水太高了!!!!'); WHEN e_deleteEmpid_exception THEN dbms_output.put_line('違反完整性約束條件,因此不能刪除此使用者!!!'); WHEN OTHERS THEN dbms_output.put_line('發生其他型別的異常了!!!'); END; /* 異常知識點的練習 */ --通過 select ... into ... 查詢某人的工資, 若沒有查詢到, 則輸出 "未找到資料" DECLARE v_salary employees.salary%TYPE; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = -666; dbms_output.put_line('salary = ' || v_salary); EXCEPTION WHEN no_data_found THEN dbms_output.put_line('查無此人,未找到資料'); WHEN OTHERS THEN dbms_output.put_line('其他型別的異常'); END; --更新指定員工工資,如工資小於300,則加100;對 NO_DATA_FOUND 異常, TOO_MANY_ROWS 進行處理 DECLARE v_salary employees.salary%TYPE; v_employee_id employees.employee_id%TYPE := 100; BEGIN --v_employee_id := 100; v_employee_id := 8899; SELECT salary INTO v_salary FROM employees WHERE employee_id = v_employee_id; --employee_id < v_employee_id; dbms_output.put_line('salary = ' || v_salary); IF v_salary < 300 THEN UPDATE employees SET salary = salary + 100 WHERE employee_id = v_employee_id; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('查無此人,未找到資料'); WHEN TOO_MANY_ROWS THEN dbms_output.put_line('輸出的行數太多了!!!'); WHEN OTHERS THEN dbms_output.put_line('其他型別的異常!!!'); END; --自定義異常: 更新指定員工工資,增加100;若該員工不存在則丟擲使用者自定義異常: no_result DECLARE --自定義異常 E_NO_RESULT EXCEPTION; BEGIN UPDATE EMPLOYEES SET SALARY = SALARY + 100 WHERE EMPLOYEE_ID = 1001; --使用隱式遊標, 丟擲自定義異常 IF SQL%NOTFOUND THEN RAISE E_NO_RESULT; END IF; EXCEPTION --處理程式丟擲的異常 WHEN E_NO_RESULT THEN DBMS_OUTPUT.PUT_LINE('更新失敗'); END;