PLSQL儲存過程呼叫儲存過程對異常的處理問題

2021-09-22 10:33:22 字數 1593 閱讀 7818

開始

如果對異常處理不正確,有可能會導致意想不到的結果。下面這個就是:

先寫兩個儲存過程 test01 和  test02:

create

orreplace

procedure test01 is

v_id emp.empno

%type;

begin

update emp set ename=

'test01

'where empno=

7369

;

/*this will raise no_data_found exception

*/select empno into v_id from emp where empno=

12345

; end

;create

orreplace

procedure test02 is

begin

update emp set ename=

'test02

'where empno=

7499;

/*call another procedure

*/test01;

exception

when no_data_found then

dbms_output.put_line(

'execption occured.!');

end;

test02 呼叫 test01 ,由於 test01處理sql文的時候,select 語句試圖查詢一條並不存在的記錄,導致 no_data_found 發生。

而這個異常實際上是發生在test01 內部,但是test01 沒有處理這個異常的**邏輯,所以就拋給了test02。

然後,按照 oracle 文件的說法:

test02 處理完此 no_data_found 異常後,因為異常已經處理完畢,所以test01的異常之前的**並沒有rollback,而是繼續和 test02 中的語句一起參與運算,最終一起被提交。

所以查詢結果是:

empno 為 7369 和 7499 的資料都被改變了!

不過,目前這個結論還不太可靠,因為,單純的異常並沒使得sql失效。比如下例:

create

orreplace

procedure test03 is

v_id emp.empno

%type;

begin

update emp set ename=

'test03

'where empno=

7369

;

/*this will raise no_data_found exception

*/select empno into v_id from emp where empno=

12345;

exception

when others then

dbms_output.put_line(

'exception occured!');

end;

結束

PL SQL儲存過程

or replace 建立或替換,如果存在就替換,不存在就建立create or replace procedure piscursor cisselect from dept2 for update beginfor row record in c loopif row record.deptno...

pl sql 儲存過程

在這段時間的開發中資料庫用的是oracle以前用的都是mssql它們的儲存過程的寫法還有一點不一樣,所以花了一天的時間看了看!以下是我做的乙個小例子!create table mytesttable id number,name varchar2 10 insert into mytesttable...

PL SQL 儲存過程

1 游標的設計開發 什麼是游標,為什麼用游標,怎樣使用游標 2 儲存過程 儲存過程的建立,引數使用,儲存過程的執行 3 儲存函式的設計 函式的建立,引數使用,函式的呼叫 4 包的設計與應用 什麼是包,包的建立及使用 儲存過程 建立語法 create or replace procedure proc...