Oracle PL SQL異常處理

2021-07-07 10:11:01 字數 3455 閱讀 3641

case語句語法格式如下:

case

《變數》

when

《表示式1> then 值1

when

《表示式2> then 值2 ……

when

《表示式n> then 值n

else 值n + 1

end;

1、使用case語句寫乙個pl/sql塊,要求輸入員工編號,根據員工的職位進行工資提公升,提公升要求如下:

如果職位是clerk,工資增長5%,

如果職位是salesman,工資增長8%,

如果職位是manager,工資增長10%,

如果職位是analyst,工資增長20%,

如果職位是president,工資不增長。

declare

v_job emp.job%type;

v_empno emp.empno%type;

v_sal emp.sal%type;

begin

v_empno := &員工編號;

select job,sal into v_job,v_sal from emp where empno = v_empno;

case v_job

when

'clerk'

then

v_sal := v_sal * 1.05;

when

'salesman'

then

v_sal := v_sal * 1.08;

when

'manager'

then

v_sal := v_sal * 1.1;

when

'analyst'

then

v_sal := v_sal * 1.2;

when

'president'

then

v_sal := v_sal;

else

dbms_output.put_line('others'||v_sal);

end case;

update emp set sal=v_sal where empno = v_empno;

dbms_output.put_line('sal='||v_sal);

end;

異常實驗:

2、處理異常—-找不到資料

我們要寫乙個pl/sql塊,功能就是由鍵盤輸入員工編號,輸出員工姓名。

如果員工編號輸入有誤,那麼就會找不到資料。如下圖所示,輸入7788,輸出員工姓名,輸入9999,沒有該員工,輸出查無此人。

思路:先寫乙個沒有exception模組的plsql塊,輸入乙個不存在的員工編號,看看會有什麼錯誤提示。然後把這個錯誤提示對應的錯誤**加入到exception模組中。

v_empno := &員工編號;

select * into v_emp from emp where empno = v_empno;

dbms_output.put_line(v_emp.empno||' '||v_emp.ename);

exception

when no_data_found then

dbms_output.put_line('不存在此項資料') ;

end;

/3、定義乙個pl/sql塊為dept表增加部門資訊

比如:鍵盤輸入乙個部門資訊,要麼異常要麼正常插入到dept表。

60,defence,shanghai

如果插入的部門在dept裡有,要引發異常,異常部分輸出「該部門已有」

如果插入的部門在dept裡沒有,正常插入這一行資訊,並顯示「部門資訊已經插入」

方案一:

利用系統提供的主鍵約束異常,僅用於存在唯一性約束的表

試圖破壞乙個唯一性約束異常 dup_val_on_index

方案二:

1.使用者自定義異常,判斷該表中是否已存在該項資料

2.insert

優點:可用於任意列,不需要滿足唯一性約束

//方案一

declare

exc_insert exception;

v_dept dept%rowtype;

v_deptcount number(4);

begin

v_dept.deptno := upper('&部門編號');

v_dept.dname := upper('&部門名稱');

v_dept.loc := upper('&部門職位');

select

count(*) into v_deptcount from emp;

insert

into dept values(v_dept.deptno,v_dept.dname,v_dept.loc);

dbms_output.put_line('部門資訊已經插入');

exception

when dup_val_on_index then

dbms_output.put_line('該項已存在,不可插入');

end;

/

//方案二

declare

exc_insert exception;

v_dept dept%rowtype;

v_deptcount number(4);

begin

v_dept.deptno := &部門編號;

v_dept.dname := &部門名稱;

v_dept.loc := &部門職位;

select count(*) into v_deptcount from emp;

select count(*) v_deptcount into

from dept where deptno = v_dept.deptno;

if v_deptcount = 0

then

reise exc_insert;

endif;

exception

when exc_insert then

dbms_output.put_line('erros');

end;

1.關於case語句的使用

2.關於使用者輸入適配upper('&部門編號');

3.關於使用者自定義異常的使用

Oracle PL SQL異常處理

oracle的exception init編譯指令declare exception name exception pragma exception init exception name,error code 下面給乙個例項 declare v num number sv num v result...

ORACLE PLSQL 異常處理

例一 create table testerr id number 10 name varchar2 10 insert into testerr values 1,test insert into testerr values 2,test insert into testerr values 3...

Oracle plsql異常處理

oracle中出現錯誤的情形通常分為編譯時錯誤 compile timeerror 和執行時錯誤 run time error 異常是在pl sql執行過程中出現的警告或錯誤。異常是如何觸發的?發生了乙個 oracle 錯誤時 使用raise語句顯式觸發 如何處理異常?用處理機截獲 在呼叫環境中傳播...