Skip to Main Content
  • Questions
  • Tracking rejected rows (while using direct insert/update/merge)

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Anand.

Asked: November 24, 2015 - 7:09 am UTC

Last updated: November 27, 2015 - 2:38 am UTC

Version: Oracle 11g

Viewed 10K+ times! This question is

You Asked

Hi,
I am facing an issue to get rejected records while using direct insert/update/merge in my PL/SQL code.

Please find my code below:
PROCEDURE TMP_PROC_INSERT_1 AS
BEGIN
TMP_EXEC_LOG('TMP_PROC_INSERT_1', 'I', SYSDATE, 'OK'); --This procedure will track TMP_PROC_INSERT_1 execution details

EXECUTE IMMEDIATE 'TRUNCATE TABLE TMP_TARGET_TABLE_1 DROP STORAGE';

INSERT INTO TMP_TARGET_TABLE_1
(
EMP_ID,
EMP_NAME
)
SELECT
EMP_ID,
EMP_NAME
FROM
TMP_SOURCE_TABLE_1;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
TMP_EXEC_LOG('TMP_PROC_INSERT_2','U',SYSDATE,'KO');
TMP_ERROR_TABLE('TMP_TARGET_TABLE_1',SQLERRM,SQLCODE,SYSDATE,user); --This procedure will track TMP_PROC_INSERT_1 error details.
END TMP_PROC_INSERT_1;

Requirements:
1.We need to load our target table via DIRECT INSERT.
2.If any rows got rejected then we should keep those rejected row in another table(log table) .
3.And also we need to track the EXECUTION & ERROR details.

Ways we tried :
1.We tried DBMS_ERRLOG.create_error_log to overcome this issue.
-> This option helped us to take backup of REJECTED ROWS
-> But it failed to track EXECUTION & ERROR details(because it skips EXCEPTION block)

Please help me overcome this issue.

and Chris said...

Log errors records the exception directly in the err$_log table, so you can read these from there. Error logging also lets you specify a tag for the exceptions. You can pass a string of the date + user to help you identify when the errors happened.

create table t (
  x varchar2(3)
);

begin
  DBMS_ERRLOG.create_error_log (dml_table_name => 't');
end;
/

insert into t 
  select lpad('x', level, 'x') from dual
  connect by level <= 5
  log errors into err$_t (to_char(sysdate, 'dd/mm/yyyy hh24:mi') || '-' || user) 
  reject limit unlimited;
  
select x, ora_err_mesg$, ora_err_tag$
from   err$_t;

X     ORA_ERR_MESG$                                                                    ORA_ERR_TAG$            
----- -------------------------------------------------------------------------------- -------------------------
xxxx  ORA-12899: value too large for column "CHRIS"."T"."X" (actual: 4, maximum: 3)    24/11/2015 10:30-CHRIS                                                                                                                   
xxxxx ORA-12899: value too large for column "CHRIS"."T"."X" (actual: 5, maximum: 3)    24/11/2015 10:30-CHRIS   

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Error logging on Direct path dmls

Rajeshwaran, Jeyabal, November 25, 2015 - 1:09 pm UTC

Requirements:
1.We need to load our target table via DIRECT INSERT.
2.If any rows got rejected then we should keep those rejected row in another table(log table) .


Direct path operation with unique constraint (ORA-0001) violation, will not be error logged.

rajesh@ORA11G> create table t(x int primary key,
  2     y varchar2(30)) ;

Table created.

rajesh@ORA11G>
rajesh@ORA11G> exec dbms_errlog.create_error_log('T');

PL/SQL procedure successfully completed.

rajesh@ORA11G> insert /*+ append */ into t(x,y)
  2  select trunc(rownum/2),rpad(object_name,30,'*')
  3  from all_objects
  4  where rownum <=5
  5  log errors into err$_t
  6  reject limit unlimited ;
insert /*+ append */ into t(x,y)
*
ERROR at line 1:
ORA-00001: unique constraint (RAJESH.SYS_C0012648) violated


rajesh@ORA11G> commit;

Commit complete.

rajesh@ORA11G> select count(*) from t;

  COUNT(*)
----------
         0

1 row selected.

rajesh@ORA11G> select count(*) from err$_t;

  COUNT(*)
----------
         0

1 row selected.

rajesh@ORA11G>

Chris Saxon
November 25, 2015 - 1:21 pm UTC

Yup, this is documented

Search for "Restrictions on DML Error Logging" in the documentation.

Technical reason

Rajeshwaran, Jeyabal, November 26, 2015 - 5:33 am UTC

Chris - Can you help me to understand the technical reason behind the unique constraint error on direct path load not supported via dml error logging ?

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library