본문 바로가기

Database/PLSQL

120 - [Oracle PL/SQL] DML logs using package dbms_errlog

데이터 입력시 오류가 발생할때 어떻게 관리하는것이 좋은지 알아보겠습니다.

107개의 데이터가 있습니다.

 

select employee_id , first_name||' '||last_name name
from employees;
/*
107 rows selected. 
*/

 

 

 

  • 테스트 준비
drop table emp_c;
/
create table emp_c
( employee_id number,
 ename varchar2(15)
 );


select * from emp_c;
/*
no rows selected
*/

 

 

 

  • 오류가 발생하면 롤백이 발생하면서 정상적인 데이터도 입력이 되지 않는다
insert into emp_c
select employee_id , first_name||' '||last_name name
from employees;
/*
Error report -
SQL Error: ORA-12899: value too large for column "HR"."EMP_C"."ENAME" (actual: 16, maximum: 15)
12899. 00000 -  "value too large for column %s (actual: %s, maximum: %s)"
*Cause:    An attempt was made to insert or update a column with a value
           which is too wide for the width of the destination column.
           The name of the column is given, along with the actual width
           of the value, and the maximum allowed width of the column.
           Note that widths are reported in characters if character length
           semantics are in effect for the column, otherwise widths are
           reported in bytes.
*Action:   Examine the SQL statement for correctness.  Check source
           and destination column data types.
           Either make the destination column wider, or use a subset
           of the source column (i.e. use substring).
*/


-- 이전 명령어에서 오류가 발생하여 입력된 데이터가 없음
select * from emp_c;
/*
no rows selected
*/

 

 

 

  • 패키지의 프로시저를 이용해서 오류를 감시할 테이블을 지정
---the solution, 테이블 이름을 파라미터로 전달한다
exec dbms_errlog.create_error_log('emp_c');


desc err$_emp_c
/*
Name            Null? Type           
--------------- ----- -------------- 
ORA_ERR_NUMBER$       NUMBER         
ORA_ERR_MESG$         VARCHAR2(2000) 
ORA_ERR_ROWID$        UROWID         
ORA_ERR_OPTYP$        VARCHAR2(2)    
ORA_ERR_TAG$          VARCHAR2(2000) 
EMPLOYEE_ID           VARCHAR2(4000) 
ENAME                 VARCHAR2(4000) 
*/

 

 

 

  • 테스트
insert into emp_c
select employee_id , first_name||' '||last_name name
from hr.employees
log errors reject limit unlimited;
-- record all the errors that the code faced in this statement into this table
-- 여기에서는 오류가 발생한 데이터만 err$_emp_c 테이블에 오류 정보가 저장되고
-- 정상 데이터들은 모두 emp_c 테이블에 입력이 된다
/*
96 rows inserted. 96개의 오류가 발생하였음
*/


-- 오류 테이블에는 11개의 오류가 저장되어 있음
select * from err$_emp_c;

 

 

  • 조회 결과는 아래와 같다