oracle異常處理

2022-03-31 22:01:05 字數 3988 閱讀 9882

1、預定義的異常處理

例1:更新指定員工工資,如工資小於1500,則加100;

declare

v_empno employees.employee_id%type := &empno;

v_sal   employees.salary%type;

begin

select salary into v_sal from employees where employee_id = v_empno;

if v_sal<=1500 then

update employees set salary = salary + 100 where employee_id=v_empno;

dbms_output.put_line('編碼為'||v_empno||'員工工資已更新!');    

else

dbms_output.put_line('編碼為'||v_empno||'員工工資已經超過規定值!');

end if;

exception

when no_data_found then 

dbms_output.put_line('資料庫中沒有編碼為'||v_empno||'的員工');

when too_many_rows then

dbms_output.put_line('程式執行錯誤!請使用游標');

when others then

dbms_output.put_line(sqlcode||'---'||sqlerrm);

end;

2、非預定義的異常處理(對於這類異常情況的處理,首先必須對非定義的oracle錯誤進行定義.)步驟如下:

1. 在pl/sql 塊的定義部分定義異常情況(異常情  exception;)

2. 將其定義好的異常情況,與標準的oracle錯誤聯絡起來,使用exception_init語句:

pragma exception_init(異常情, 錯誤代);

3. 在pl/sql 塊的異常情況處理部分對異常情況做出相應的處理。

例2:刪除指定部門的記錄資訊,以確保該部門沒有員工。

insert into departments values(50, 'finance', 'chicago');

declare

v_deptno departments.department_id%type := &deptno;

deptno_remaining exception;

pragma exception_init(deptno_remaining, -2292);

/* -2292 是違反一致性約束的錯誤** */

begin  

delete from departments where department_id = v_deptno;

exception

when deptno_remaining then

dbms_output.put_line('違反資料完整性約束!');

when others then

dbms_output.put_line(sqlcode||'---'||sqlerrm);

end; 

3、使用者自定義的異常處理

1. 在pl/sql 塊的定義部分定義異常情況:

異常情  exception;2. raise 異常情;3. 在pl/sql 塊的異常情況處理部分對異常情況做出相應的處理。

例3:更新指定員工工資,增加100;

declare

v_empno employees.employee_id%type :=&empno;

no_result  exception;

begin

update employees set salary = salary+100 where employee_id = v_empno;

if sql%notfound then

raise no_result;

end if;

exception

when no_result then

dbms_output.put_line('你的資料更新語句失敗了!');

when others then

dbms_output.put_line(sqlcode||'---'||sqlerrm);

end;

4、使用者定義的異常處理

例4:建立乙個函式get_salary, 該函式檢索指定部門的工資總和,其中定義了-20991和-20992號錯誤,分別處理引數為空和非法部門**兩種錯誤:

create table errlog(

errcode number,

errtext char(40));

create or replace function get_salary(p_deptno number)

return number

as   v_sal number;

begin

if p_deptno is null then

raise_application_error(-20991, 』部門**為空』);

elsif p_deptno<0 then

raise_application_error(-20992, 』無效的部門**』);

else

select sum(employees.salary) into v_sal from employees

where employees.department_id=p_deptno;

return v_sal;

end if;

end;

declare

v_salary number(7,2);

v_sqlcode number;

v_sqlerr varchar2(512);

null_deptno exception;

invalid_deptno exception;

pragma exception_init(null_deptno,-20991);

pragma exception_init(invalid_deptno, -20992);

begin

v_salary :=get_salary(10);

dbms_output.put_line('10號部門工資:' || to_char(v_salary));

begin

v_salary :=get_salary(-10);

exception

when invalid_deptno then

v_sqlcode :=sqlcode;

v_sqlerr  :=sqlerrm;

insert into errlog(errcode, errtext)

values(v_sqlcode, v_sqlerr);

commit;

end inner1;

v_salary :=get_salary(20);

dbms_output.put_line('部門號為20的工資為:'||to_char(v_salary));

begin

v_salary :=get_salary(null);

end inner2;

v_salary := get_salary(30);

dbms_output.put_line('部門號為30的工資為:'||to_char(v_salary));

exception

when null_deptno then

v_sqlcode :=sqlcode;

v_sqlerr  :=sqlerrm;

insert into errlog(errcode, errtext) values(v_sqlcode, v_sqlerr);

commit;

when others then

dbms_output.put_line(sqlcode||'---'||sqlerrm);

end outer;

ORACLE 異常處理

一 開發pl sql程式時,需要考慮到程式執行時可能出現的各種異常,當異常出現時,或是中斷程式執行,或是使程式從錯誤中恢復,從而繼續執行。常用的異常型別有 no data found 沒有發現資料 too many rows select into 語句查詢結果有多個資料行 others 可以捕捉所...

Oracle 異常處理

1 什麼是異常 在pl sql 中的乙個警告或錯誤的情形都可被稱為異常。包括編譯時錯誤 pls 和執行時錯誤 ora 乙個異常通常包含乙個錯誤 和錯誤文字,分別指示異常的編號和具體錯誤資訊。異常情況處理 exception 是用來處理正常執行過程中未預料的事件,程式塊的異常處理預定義的錯誤和自定義錯...

Oracle 異常處理

異常處理 處理程式不可意料的操作,防止程式崩潰,起到友好提示 語法 exception when 異常型別 then 異常處理 異常型別 處理相關的異常 others 處理所有的異常 no data found 沒有找到資料 too many rows 返回資料行數過多自定義異常 實行彈窗的方式提示...