Oracle 儲存過程中自定義異常

2021-07-27 04:57:26 字數 3319 閱讀 5966

參考:

oracle 使用者自定義異常小例子

oracle中raise異常深入分析

customize_exp exception; --自定義異常

begin

for c in (select d.* from scott.dept d) loop

begin

dbms_output.put_line('dept: ' || c.deptno || '=' || c.dname);

--當部門id為40時丟擲異常

if (c.deptno = 40) then

raise customize_exp; -- 拋出自定義異常

endif;

--當部門id為10、20、30時,會執行下面的查詢,由於出現多行所以會報 too many rows round!

--當部門id為40時,這裡不再執行,控制轉向

** 注意是 elsif 不是 else if

declare

empname varchar2(255);

begin

for c in (select d.* from scott.dept d) loop

begin

dbms_output.put_line('dept: ' || c.deptno || '=' || c.dname);

if c.deptno = 10

then

dbms_output.put_line('deptno= ' || c.deptno);

select e.ename into empname from scott.emp e where e.empno = 7782;

if empname = 'clark' then

dbms_output.put_line('ename= ' || empname);

elsif empname = 'king' then

dbms_output.put_line('ename= ' || empname);

endif; end

if; exception

when no_data_found then

dbms_output.put_line('data

isnot found!');

when too_many_rows then

dbms_output.put_line('too many rows round!');

when

others

then

dbms_output.put_line('others error');

end;

endloop;

exception

when

others

then

dbms_output.put_line('others error');

end;

並列for迴圈與區域性變數

declare

empname varchar2(255);

begin

for c in (select d.* from scott.dept d) loop

begin

dbms_output.put_line('dept: ' || c.deptno || '=' || c.dname);

exception

when no_data_found then

dbms_output.put_line('data

isnot found!');

when too_many_rows then

dbms_output.put_line('too many rows round!');

when

others

then

dbms_output.put_line('others error');

end;

endloop;

for c in (select e.* from scott.emp e) loop

--區域性變數

declare

v_ename varchar2(255);

begin

v_ename := c.ename;

dbms_output.put_line('emp: ' || v_ename);

exception

when no_data_found then

dbms_output.put_line('data

isnot found!');

when too_many_rows then

dbms_output.put_line('too many rows round!');

when

others

then

dbms_output.put_line('others error');

end;

endloop;

exception

when

others

then

dbms_output.put_line('others error');

end;

ORACLE 自定義分頁儲存過程

一 建立包 create orreplace package pkg jk lab basic istype cursor type is ref cursor procedure sp get pagination pi tablename invarchar2,表名 pi where in va...

oracle儲存過程和自定義函式

學習中遇到的相關問題plsql是什麼?資料庫的物件 表 檢視 索引 序列 同義詞 儲存過程 儲存函式。儲存過程和儲存函式 指儲存在資料庫中供所有使用者程式呼叫的子程式叫儲存過程 儲存函式。相同點 完成特定功能的程式。區別 是否用return語句返回值。儲存函式可以通過return返回值,而儲存過程不...

Oracle儲存過程中定義多個游標

1 直接定義多個顯示游標 create orreplace procedure acc.dbp realcitytrafficcnt is cursor cur1 is select 第乙個游標 cursor cur2 is select 第二個游標 begin 呼叫cur1 begin open ...