Sub topic 13
Keeping Batch Loads Going with `LOG ERRORS`
Assume you know basic INSERT, UPDATE, and SELECT statements, and that emp_demo, emp_stage, and their constraints already exist. In a batch load, one bad source row can otherwise stop the whole DML statement. Oracle's DML error logging feature lets valid rows continue while recording row-level errors in a separate table.
Create an error log table
Create the error table once for the target table with DBMS_ERRLOG.CREATE_ERROR_LOG. Oracle names the table ERR$_ followed by the target table name.
EXEC DBMS_ERRLOG.CREATE_ERROR_LOG('EMP_DEMO');The generated table contains columns such as:
ORA_ERR_NUMBER$, the Oracle error number.ORA_ERR_MESG$, the error message.ORA_ERR_ROWID$, the affected row's row ID when available.ORA_ERR_OPTYP$, the operation type.ORA_ERR_TAG$, an optional label for the batch.- Copies of the target table's columns.
For example, this batch attempts to insert every row from the staging table:
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary, status)
SELECT emp_id, emp_name, dept_id, salary, status
FROM emp_stage
LOG ERRORS INTO err$_emp_demo ('load_1')
REJECT LIMIT UNLIMITED;Rows that violate a row-level rule, such as a duplicate primary key or an invalid foreign key, are written to ERR$_EMP_DEMO. Other rows are inserted normally. The tag load_1 identifies this particular load.
Query the errors after the statement:
SELECT ora_err_number$,
ora_err_mesg$,
emp_id,
emp_name,
ora_err_tag$
FROM err$_emp_demo
WHERE ora_err_tag$ = 'load_1';The successful changes are still part of the current transaction. Commit them only after reviewing the error count and messages.
Choose a reject limit
REJECT LIMIT controls how many row errors Oracle accepts before stopping the statement.
REJECT LIMIT 0stops at the first error.REJECT LIMIT 25permits up to 25 rejected rows.REJECT LIMIT UNLIMITEDcontinues until Oracle processes all source rows.
The limit applies to the statement, not to each source row indefinitely. Use a finite limit when a high error rate suggests that the source data or mapping is wrong.
Scope and common mistakes
You can attach LOG ERRORS to INSERT, UPDATE, DELETE, and MERGE. It handles errors associated with individual rows. It does not guarantee that every possible failure can be logged. For example, a statement-level failure, a missing error table, or a transaction problem can still abort the statement.
The error table also does not replace source-data validation. Review the logged messages, correct the source rows, and rerun only the rejected data when possible. Avoid using UNLIMITED without monitoring the number of failures.
Exercise
What happens to valid rows when one source row violates a primary key during an INSERT ... SELECT with LOG ERRORS and REJECT LIMIT UNLIMITED?
Show answer
The valid rows are inserted as part of the current transaction, while the invalid row and its error details are written to the error table. The changes still require COMMIT.