PL SQL程式設計學習之異常處理

2021-07-09 19:40:16 字數 3030 閱讀 9503

新增外來鍵關聯:

alter table dept_learn add constraint pk_dept_deptid primary key (department_id);

alter table emp_learn add constraint fk_emp_dept_deptid foreign key (department_id) references dept_learn(department_id);

在pl/sql塊中捕獲並處理異常,可以提高程式的健壯性,使得應用程式可以安全正常的執行。異常是一種pl/sql識別符號,它有預定義異常、非預定義異常和自定義異常三種型別。在pl/sql塊中,如果不捕獲和處理異常,oracle會將錯誤傳遞到呼叫環境。

本節學習目標:

declare

v_name emp_learn.first_name%type;

begin

select first_name into v_name from emp_learn where department_id = &dno;

dbms_output.put_line('雇員號'||&dno||'的名字為:'||v_name);

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;

非預定義異常用於處理與預定義異常無關的oracle錯誤。預定義異常只能處理21種oracle錯誤,而pl/sql塊可能還會遭遇其它oracle錯誤,此時可能需要用到非預定義異常了。

非預定義異常使用步驟:

a)定義異常識別符號。必須在定義部分定義異常識別符號。

b)在oracle錯誤號和異常之間建立關聯。需要在定義部分引用偽過程exception_init。

c)捕捉並處理異常。

示例:在此節的開始時,我們為emp_learn和dept_learn變建立了外來鍵關聯,當更新雇員的部門號時,部門號必須在dept_learn表中存在,否則會觸發ora-02291錯誤。

-- 觸發ora-02291錯誤

sql> update emp_learn set department_id=2000 where employee_id = 198;

ora-02291: 違反完整約束條件 (hr.fk_emp_dept_deptid) - 未找到父項關鍵字

-- 使用非預定義異常處理ora-02291錯誤

declare

e_int exception;

pragma exception_init(e_int,-2291);

v_name emp_learn.first_name%type:=lower('&name');

v_deptno dept_learn.department_id%type:=&dno;

begin

update emp_learn set department_id=v_deptno where lower(first_name) = v_name;

exception

when e_int then

dbms_output.put_line('該部門不存在!');

end;

在上面「使用非預定義異常處理ora-02291錯誤」的示例**中,如果輸入乙個不存在的雇員的first_name,將不會更新到行資料,不會觸發e_int這個非預定義異常,pl/sql將不會給出任何提示資訊。如果此時需要獲取到某些資訊,或者做某些操作的話,就需要用到自定義異常了。

使用步驟:

a)定義異常識別符號。必須在定義部分定義。

b)主動觸發異常。使用raise語句顯式觸發。

c)捕獲並處理異常。

示例:

declare

e_int exception;

e_int_norows exception;

pragma exception_init(e_int,-2291);

v_name emp_learn.first_name%type:=lower('&name');

v_deptno dept_learn.department_id%type:=&dno;

begin

update emp_learn set department_id=v_deptno where lower(first_name) = v_name;

if sql%notfound then

raise e_int_norows;

endif;exception

when e_int then

dbms_output.put_line('該部門不存在!');

when e_int_norows then

dbms_output.put_line('該雇員不存在!');

end;

示例:

declare

e_int exception;

pragma exception_init(e_int,-20000);

begin

delete

from dept_learn where department_id=&dno;

if sql%notfound then

endif;exception

when e_int then

dbms_output.put_line('錯誤號:'||sqlcode||',錯誤訊息:'||sqlerrm);

when others then

dbms_output.put_line('others-錯誤號:'||sqlcode||',錯誤訊息:'||sqlerrm);

end;

PL SQL學習筆記 異常處理

一 預定義異常錯誤 先看 declare mytitle labor.xland.title type begin select title into mytitle from labor.xland where state 2 dbms output.put line mytitle except...

pl sql異常處理

丟擲異常 oracle有三種型別的異常錯誤 1 預定義 predefined 異常 oracle預定義的異常情況大約有24個。對這種異常情況的處理,無需在程式中定義,由oracle自動將其引發。2 非預定義 predefined 異常 即其他標準的oracle錯誤。對這種異常情況的處理,需要使用者在...

PLSQL 異常處理

1.異常塊begin pl sql塊 exception when no data found then 沒有找到資料 響應命令 when too many rows then 返回多行,隱式游標每次只能檢索一行資料 響應命令 when invalid number then 字元向數字轉換失敗 響...