ORACLE SAVE EXCEPTION 子句

來源:互聯網
上載者:User

今天在一個文章裡看到shiyiwan的回帖中提及到了兩個自己以前沒見過的概念,save exception和dml error logging。上網搜了搜相關內容,看了看大概明白意思,不過在實際運用中還是沒怎麼用過。儲存下來,以後用的到的話方便查閱。

這一篇是關於save exception的,另外一篇dml error logging的參見如下連結

http://blog.csdn.net/wh62592855/archive/2009/11/13/4808012.aspx

 

==================================================================================

 

Oracle 從9i 開始增強了批 DML 在發生異常時處理的錯誤及失敗的程式執行能力。這是通過FORALL 語句的 SAVE EXCEPTION 子句來實現,其文法如下:

FORALL index in lower..upper save exceptions

SAVE EXCEPTIONS 子句是 oracle 9i 中引入的,它在一個隱式遊標屬性,SQL%BULK_EXCEPTIONS 中儲存錯誤行,允許 FORALL 語句繼續處理其餘行。

下面這一段代碼是 SQL%BULK_EXCEPTIONS 的應用例子:

 

create or replace procedure batch_dml(errnum outnumber,errtext outvarchar2)
is

type user_id_tab is table of number index by binary_integer;
type user_name_tab is table of varchar2( 20 ) index by binary_integer;
type user_sex_tab is table of varchar2( 2 ) index by binary_integer;
 
user_id user_id_tab;
user_name user_name_tab;
user_sex user_sex_tab;

bulk_bind_excep EXCEPTION;
pragma exception_init(bulk_bind_excep,- 24381 );
begin
for idx in 1 .. 50000 loop

user_id(idx):=idx;
user_name(idx):= 'xxx' ||idx;
user_sex(idx):= 'F' ;
endloop;
user_id( 40000 ):= 39999 ;
user_id( 10000 ):= 9999 ;
delete from t_user;
forall idx in user_id.first..user_id.last save exceptions
insert into t_user values(user_id(idx), user_name(idx),user_sex(idx));
errnum:= 0 ;
errtext:= '' ;
exception
when bulk_bind_excep then
for i in 1 ..sql%bulk_exceptions.count loop
dbms_output.put_line( 'Iteration '
||SQL%bulk_exceptions(i).error_index|| 'failed with error '
||sqlerrm(sql%bulk_exceptions(i).error_code));
endloop;
commit;
when others then
commit;
errnum:=sqlcode;
errtext:=sqlerrm;
end batch_dml;

這個例子是修改了上面的程式,加上 save exceptions 異常處理,當批 dml發生異常時也能正常運行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.