PL/SQL 異常錯誤處理,plsql錯誤處理
異常錯誤處理
一個優秀的程式都應該能夠正確處理各種出錯情況,並儘可能從錯誤中恢複。ORACLE 提供異常情況(EXCEPTION)和異常處理(EXCEPTION HANDLER)來實現錯誤處理
①有三種類型的異常錯誤:
1. 預定義( Predefined )錯誤
ORACLE預定義的異常情況大約有24個。對這種異常情況的處理,無需在程式中定義,由ORACLE自動將其引發。
2. 非預定義( Predefined )錯誤
即其他標準的ORACLE錯誤。對這種異常情況的處理,需要使用者在程式中定義,然後由ORACLE自動將其引發。
3. 使用者定義(User_define) 錯誤
程式執行過程中,出現編程人員認為的非正常情況。對這種異常情況的處理,需要使用者在程式中定義,然後顯式地在程式中將其引發。
②異常處理部分一般放在PL/SQL 程式體的後半部,結構為:
EXCEPTION
WHEN first_exception THEN<code to handle first exception >
WHEN second_exception THEN<code to handle second exception >
WHEN OTHERS THEN <code to handle others exception >
END;
異常處理可以按任意次序排列,但OTHERS 必須放在最後.
③預定義的異常處理
對這種異常情況的處理,只需在PL/SQL塊的異常處理部分,直接引用相應的異常情況名,並對其完成相應的異常錯誤處理即可。
[預定義異常]
declare
v_sal employees.salary%type;
begin
select salary into v_sal
from employees
where employee_id >100;
dbms_output.put_line(v_sal);
exception
when Too_many_rows then dbms_output.put_line('輸出的行數太多了');
end;
④非預定義的異常處理
對於這類異常情況的處理,首先必須對非定義的ORACLE錯誤進行定義。步驟如下:
1. 在PL/SQL 塊的定義部分定義異常情況:
<異常情況> EXCEPTION;
2. 將其定義好的異常情況,與標準的ORACLE錯誤聯絡起來,使用PRAGMA EXCEPTION_INIT 語句:
PRAGMA EXCEPTION_INIT(<異常情況>, <錯誤碼>);
3. 在PL/SQL 塊的異常情況處理部分對異常情況做出相應的處理。
[非預定義異常]
declare
v_sal employees.salary%type;
--聲明一個異常
delete_mgr_excep exception;
--把自訂的異常和oracle的錯誤關聯起來
PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
begin
delete from employees
where employee_id = 100;
select salary into v_sal
from employees
where employee_id >100;
dbms_output.put_line(v_sal);
exception
when Too_many_rows then dbms_output.put_line('輸出的行數太多了');
when delete_mgr_excep then dbms_output.put_line('Manager不能直接被刪除');
end;
⑤使用者自訂的異常處理
當與一個異常錯誤相關的錯誤出現時,就會隱含觸發該異常錯誤。使用者定義的異常錯誤是通過顯式使用RAISE語句來觸發。當引發一個異常錯誤時,控制就轉向到EXCEPTION塊異常錯誤部分,執行錯誤處理代碼。
對於這類異常情況的處理,步驟如下:
1. 在PL/SQL 塊的定義部分定義異常情況:
<異常情況> EXCEPTION;
2. RAISE <異常情況>;
3. 在PL/SQL 塊的異常情況處理部分對異常情況做出相應的處理。
[使用者自訂異常]
declare
v_sal employees.salary%type;
--聲明一個異常
delete_mgr_excep exception;
--把自訂的異常和oracle的錯誤關聯起來
PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
--聲明一個異常
too_high_sal exception;
begin
select salary into v_sal
from employees
where employee_id =100;
if v_sal > 1000 then
raise too_high_sal;
end if;
delete from employees
where employee_id = 100;
dbms_output.put_line(v_sal);
exception
when Too_many_rows then dbms_output.put_line('輸出的行數太多了');
when delete_mgr_excep then dbms_output.put_line('Manager不能直接被刪除');
--處理異常
when too_high_sal then dbms_output.put_line('工資過高了');
end;
⑥在PL/SQL 中使用SQLCODE, SQLERRM
SQLCODE 返回錯誤碼數字
SQLERRM 返回錯誤資訊.
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE||’---‘||SQLERRM);
⑦異常程式:
1.通過 select ... into ... 查詢某人的工資, 若沒有查詢到, 則輸出 "未找到資料"
declare
--定義一個變數
v_sal employees.salary%type;
begin
--使用 select ... into ... 為 v_sal 賦值
select salary into v_sal from employees where employee_id = 1000;
dbms_output.put_line('salary: ' || v_sal);
exception
when No_data_found then
dbms_output.put_line('未找到資料');
end;
或
declare
--定義一個變數
v_sal employees.salary%type;
begin
--使用 select ... into ... 為 v_sal 賦值
select salary into v_sal from employees;
dbms_output.put_line('salary: ' || v_sal);
exception
when No_data_found then
dbms_output.put_line('未找到資料!');
when Too_many_rows then
dbms_output.put_line('資料過多!');
end;
2. 更新指定員工工資,如工資小於300,則加100;對 NO_DATA_FOUND 異常, TOO_MANY_ROWS 進行處理.
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id = 100;
if(v_sal < 300) then
update employees set salary = salary + 100 where employee_id = 100;
else
dbms_output.put_line('工資大於300');
end if;
exception
when no_data_found then
dbms_output.put_line('未找到資料');
when too_many_rows then
dbms_output.put_line('輸出的資料行太多');
end;
3. 處理非預定義的異常處理: "違反完整約束條件"
declare
--1. 定義異常
temp_exception exception;
--2. 將其定義好的異常情況,與標準的 ORACLE 錯誤聯絡起來,使用 EXCEPTION_INIT 語句
PRAGMA EXCEPTION_INIT(temp_exception, -2292);
begin
delete from employees where employee_id = 100;
exception
--3. 處理異常
when temp_exception then
dbms_output.put_line('違反完整性條件約束!');
end;
4. 自訂異常: 更新指定員工工資,增加100;若該員工不存在則拋出使用者自訂異常: no_result
declare
--自訂異常
no_result exception;
begin
update employees set salary = salary + 100 where employee_id = 1001;
--使用隱式遊標, 拋出自訂異常
if sql%notfound then
raise no_result;
end if;
exception
--處理常式拋出的異常
when no_result then
dbms_output.put_line('更新失敗');
end;