用dbms_error記錄load時的錯誤
當我們向一個表中load(insert)資料時,如果遇到錯誤(如,違反了PK),這時語句則會終止並rollback所有.這樣既浪費了時間又佔了系統資源.那麼有沒有辦法,當遇到錯誤時不讓語句終止,讓其跳過錯誤繼續執行並將其錯誤記錄下來呢?
答案是肯定的,下面請看測試案例.
YXYUP@dbatest>desc t_test;
Name Null? Type
------------------------------------------------------------------------ -------- -------------------------------------------------
ID NOT NULL NUMBER
YXYUP@dbatest>alter table t_test drop column name;
Table altered.
YXYUP@dbatest>begin
2 for i in 1..10
3 loop
4 execute immediate 'insert into t_test values(:x)' using i;
5 end loop;
6 insert into t_test values(1);
7 commit;
8 end;
9 /
begin
*
ERROR at line 1:
ORA-00001: unique constraint (YXYUP.PK_ID) violated
ORA-06512: at line 6
YXYUP@dbatest>exec dbms_errlog.create_error_log('T_TEST') ;
PL/SQL procedure successfully completed.
---用dbms_errlog.create_error_log建立用於記錄錯誤的表,表名預設是ERR$_加上要操作的表,如下.
YXYUP@dbatest>desc err$_t_test;
Name Null? Type
------------------------------------------------------------------------ -------- -------------------------------------------------
ORA_ERR_NUMBER$ NUMBER
ORA_ERR_MESG$ VARCHAR2(2000)
ORA_ERR_ROWID$ ROWID
ORA_ERR_OPTYP$ VARCHAR2(2)
ORA_ERR_TAG$ VARCHAR2(2000)
ID VARCHAR2(4000)
YXYUP@dbatest>select * from err$_t_test;
no rows selected
YXYUP@dbatest>begin
2 for i in 1..10
3 loop
4 execute immediate 'insert into t_test values(:x)' using i;
5 end loop;
6 insert into t_test values(1);
7 commit;
8 end;
9 /
begin
*
ERROR at line 1:
ORA-00001: unique constraint (YXYUP.PK_ID) violated
ORA-06512: at line 6
YXYUP@dbatest>begin
2 for i in 1..10
3 loop
4 execute immediate 'insert into t_test values(:x)' using i;
5 end loop;
6 insert into t_test values(1)
7 LOG ERRORS
8 REJECT LIMIT UNLIMITED;
9 commit;
10 end;
11 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.04
YXYUP@dbatest>select * from t_test;
ID
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
YXYUP@dbatest> select ORA_ERR_NUMBER$,ORA_ERR_MESG$,ORA_ERR_OPTYP$ ,ID from err$_t_test
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_OPTYP$ ID
--------------- ------------------------------------------------------------ -------------------- ----------
1 ORA-00001: unique constraint (YXYUP.PK_ID) violated I 1
答案是肯定的,下面請看測試案例.
YXYUP@dbatest>desc t_test;
Name Null? Type
------------------------------------------------------------------------ -------- -------------------------------------------------
ID NOT NULL NUMBER
YXYUP@dbatest>alter table t_test drop column name;
Table altered.
YXYUP@dbatest>begin
2 for i in 1..10
3 loop
4 execute immediate 'insert into t_test values(:x)' using i;
5 end loop;
6 insert into t_test values(1);
7 commit;
8 end;
9 /
begin
*
ERROR at line 1:
ORA-00001: unique constraint (YXYUP.PK_ID) violated
ORA-06512: at line 6
YXYUP@dbatest>exec dbms_errlog.create_error_log('T_TEST') ;
PL/SQL procedure successfully completed.
---用dbms_errlog.create_error_log建立用於記錄錯誤的表,表名預設是ERR$_加上要操作的表,如下.
YXYUP@dbatest>desc err$_t_test;
Name Null? Type
------------------------------------------------------------------------ -------- -------------------------------------------------
ORA_ERR_NUMBER$ NUMBER
ORA_ERR_MESG$ VARCHAR2(2000)
ORA_ERR_ROWID$ ROWID
ORA_ERR_OPTYP$ VARCHAR2(2)
ORA_ERR_TAG$ VARCHAR2(2000)
ID VARCHAR2(4000)
YXYUP@dbatest>select * from err$_t_test;
no rows selected
YXYUP@dbatest>begin
2 for i in 1..10
3 loop
4 execute immediate 'insert into t_test values(:x)' using i;
5 end loop;
6 insert into t_test values(1);
7 commit;
8 end;
9 /
begin
*
ERROR at line 1:
ORA-00001: unique constraint (YXYUP.PK_ID) violated
ORA-06512: at line 6
YXYUP@dbatest>begin
2 for i in 1..10
3 loop
4 execute immediate 'insert into t_test values(:x)' using i;
5 end loop;
6 insert into t_test values(1)
7 LOG ERRORS
8 REJECT LIMIT UNLIMITED;
9 commit;
10 end;
11 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.04
YXYUP@dbatest>select * from t_test;
ID
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
YXYUP@dbatest> select ORA_ERR_NUMBER$,ORA_ERR_MESG$,ORA_ERR_OPTYP$ ,ID from err$_t_test
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_OPTYP$ ID
--------------- ------------------------------------------------------------ -------------------- ----------
1 ORA-00001: unique constraint (YXYUP.PK_ID) violated I 1
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7364032/viewspace-611737/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Docker環境編譯時的錯誤記錄Docker編譯
- Kafka錯誤記錄Kafka
- 記錄一次錯誤的使用當前時間new Date()引發的錯誤
- 記錄錯誤並繼續執行:錯誤事件記錄子句 --轉事件
- C++錯誤記錄C++
- SpringMVC錯誤記錄SpringMVC
- [記錄]關於安裝VMware workstation 時我遇到的錯誤
- 記錄一次根據錯誤資訊無法定位錯誤的錯誤
- MongoDB的一次錯誤記錄MongoDB
- 錯誤記錄:apache預設網頁訪問錯誤Apache網頁
- JS錯誤記錄 – dom操作 – 排序JS排序
- 假資料填充錯誤記錄
- virtualbox 錯誤解決記錄
- 記錄一次數字和字串比較時候犯的錯誤字串
- V$SESSION記錄的BLOCKING_SESSION錯誤SessionBloC
- 錯誤記錄(八)could not initialize proxy - no SessionSession
- postgresal使用錯誤解決 (記錄篇)
- Qt 錯誤記錄registered using qRegisterMetaType().QT
- 啟動idea時, 碰到"failed to load jvm DLL ..."錯誤 解決方案IdeaAIJVM
- Incorrect key file for table '/tmp/....'錯誤的解決--記錄
- Oracle RAC 錯誤記錄以及處理方法Oracle
- 用觸發器返回錯誤提示,糾正錯誤資料錄入觸發器
- 記錄在shell指令碼中使用sudo echo x > 時,拋Permission denied錯誤指令碼
- MySQL儲存函式錯誤[Err] 1064的除錯記錄MySql儲存函式除錯
- vagrant homestead 錯誤記錄以及解決方案
- php安全配置記錄和常見錯誤梳理PHP
- hadoop 安裝錯誤記錄(持續更新)Hadoop
- mysql load 相關實驗記錄MySql
- [記錄]在高光譜影像分類中遭遇的愚蠢錯誤
- 昨天裝RAC時遇到的一個錯誤Loading module "oracleasm": Unable to load module "oracleasm"[FAILED]OracleASMAI
- 記錄一次定時器報錯定時器
- 虛擬機器部署 Sentinel 服務錯誤記錄虛擬機
- 記錄redis佇列read error on connection to錯誤Redis佇列Error
- Laravel 大檔案分塊上傳錯誤記錄Laravel
- abp9 .net8 升級錯誤記錄
- 【Intellij IDEA】開啟IDEA時錯誤:Failed to load JVM DLL ...\jvm.dllIntelliJIdeaAIJVM
- 如何處理CloudFoundry應用部署時遇到的254錯誤Cloud
- 測試庫發生ora-12528錯誤及相應的該錯誤測試記錄