Oracle DML容錯處理(1)

來源:互聯網
上載者:User

標籤:comm   ignore   body   ora   自動產生   user   example   case   問題   

Oracle dml操作過程中可能出現鍵重複或者資料類型不一致等問題,一般進行資料處理時候需要對這些可能出現的錯誤提前考慮,避免更新失敗。Oralce給出了一些其他解決方案,以在不同情境下使用。

1、ignore_row_on_dupkey_index HINT

Oracle 11.2.0.1 版本中心加入的3個提示CHANGE_DUPKEY_ERROR_INDEX, IGNORE_ROW_ON_DUPKEY_INDEX, RETRY_ON_ROW_CHANGE,與其他提示不同,特別之處在於存在"語義效果(semantic effect)"。

在 insert into tablea ...select * from tbl中,如果存在唯一約束,會導致整個insert操作失敗。使用IGNORE_ROW_ON_DUPKEY_INDEX提示,會忽略唯一約束衝突,復原當前行,繼續完成其他行的插入。

樣本:

資料準備:

create table emp1(empno number primary key,ename varchar2(50));

insert into emp1(empno,ename) select empno,ename from emp;

commit;

emp1表存在empno主鍵.

再次插入:

insert into emp1(empno,ename) select empno,ename from emp;

提示錯誤:

ORA-00001: 違反唯一約束條件 (SCOTT.SYS_C0013035)

使用HINT:

insert /*+ignore_row_on_dupkey_index(emp1,SYS_C0013035)*/into emp1(empno,ename) select empno,ename from emp;

提示:插入0行;

SYS_C0013035:建立主鍵時oracle自動產生的索引。

2、使用dbms_errlog包

說明:10g後可用,不支援LONG, CLOB, BLOB, BFILE, ADT資料類型

建立錯誤記錄檔表

begin

dbms_errlog.create_error_log(dml_table_name => ‘EMP1‘,

err_log_table_name => ‘T_ERR_LOG‘,

err_log_table_owner => user,

err_log_table_space => ‘users‘,

skip_unsupported => true);

end;

參數說明:

Parameter

Description

dml_table_name

The name of the DML table to base the error logging table on. The name can be fully qualified (for example, emp, scott.emp, "EMP", "SCOTT"."EMP"). If a name component is enclosed in double quotes, it will not be upper cased.

err_log_table_name

The name of the error logging table you will create.

The default is the first 25 characters in the name of the DML table prefixed with‘ERR$_‘. Examples are the following:

dml_table_name: ‘EMP‘, err_log_table_name: ‘ERR$_EMP‘

dml_table_name: ‘"Emp2"‘, err_log_table_name: ‘ERR$_Emp2‘

err_log_table_owner

The name of the owner of the error logging table. You can specify the owner indml_table_name. Otherwise, the schema of the current connected user is used.

err_log_table_space

The tablespace the error logging table will be created in. If not specified, the default tablespace for the user owning the DML error logging table will be used.

skip_unsupported

When set to TRUE, column types that are not supported by error logging will be skipped over and not added to the error logging table.

When set to FALSE, an unsupported column type will cause the procedure to terminate.

The default is FALSE.

 

對於不支援的資料類型可以使用最後一個參數控制,如果為true,不支援類型欄位將不會進入錯誤記錄檔表。如果是false,在遇到不支援類型欄位時執行包會報錯。

各參數預設值如下:

DBMS_ERRLOG.CREATE_ERROR_LOG (

dml_table_name IN VARCHAR2,

err_log_table_name IN VARCHAR2 := NULL,

err_log_table_owner IN VARCHAR2 := NULL,

err_log_table_space IN VARCHAR2 := NULL,

skip_unsupported IN BOOLEAN := FALSE);

注意:再次執行該包會報錯,執行前須確認錯誤記錄表不存在。

dml使用錯誤記錄檔表

insert into emp1(empno,ename) select empno,ename from emp log errors into t_err_log reject limit unlimited;

注意紅色字型部分。Limimt後面可以是具體數字,表示容錯行數

文法:

LOG ERRORS [INTO [schema.]table] [(‘simple_expression‘)] [REJECT LIMIT integer|UNLIMITED]

simple_expression:用來標記t_err_log表ora_err_tag$欄位資訊

Update、merge和delete也可以使用該方法。

更新完檢查t_err_log失敗記錄及錯誤原因。

Oracle DML容錯處理(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.