Oracle/Admin

Constraint violated 시 어디서 에러가 났는지 보여주는 exceptions 테이블 사용예제

에몽이ㅋ 2012. 3. 2. 00:46
우선
desc exceptions 해보시고 테이블이 존재하지 않으면 

SQL> @?/rdbms/admin/utlexcpt   << 실행시켜주세요

Table created.

Primary key constraint를 disable한 후
자료를 입력하고 나서 다시 enable할때 에러가 난 상황

(중복되는 값 발견)   --> 에러가 나도 특정 쿼리를 써주지 않으면 exceptions 에 들어가지않습니다.
SQL> alter table scott.ERR_CODE enable constraint ERR_CODE_CODE_PK;
alter table scott.ERR_CODE enable constraint ERR_CODE_CODE_PK
*
ERROR at line 1:
ORA-02437: cannot validate (SCOTT.ERR_CODE_CODE_PK) - primary key violated

SQL> select rowid, code from scott.err_code
  2  where rowid in(select row_id from exceptions);

no rows selected


EXCEPTIONS 테이블을 사용하기 위해 
exceptions into sys.exceptions 를 쿼리에 추가

SQL> alter table scott.ERR_CODE enable constraint ERR_CODE_CODE_PK exceptions into sys.exceptions;
SQL> alter table scott.ERR_CODE enable constraint ERR_CODE_CODE_PK 
exceptions into sys.exceptions;
alter table scott.ERR_CODE enable constraint ERR_CODE_CODE_PK exceptions into sys.exceptions
*
ERROR at line 1:
ORA-02437: cannot validate (SCOTT.ERR_CODE_CODE_PK) - primary key violated

SQL> select rowid, code from scott.err_code
  2  where rowid in(select row_id from sys.exceptions);

ROWID              CODE
------------------ --------------------
AAACkVAAGAAAAAnABK 07202
AAACkVAAGAAAAAlAAB 07202
AAACkVAAGAAAAAnABL 07203
AAACkVAAGAAAAAlAAC 07203
....생략....
AAACkVAAGAAAAAlAAZ 07226
AAACkVAAGAAAAAnABs 07236
AAACkVAAGAAAAAoAAD 07240
AAACkVAAGAAAAAoAAO 07252
102 rows selected.

이후 ROWID를 이용해서 에러가 난 ROW의 값을
일일히 다 바꾸고 다시 Enable하면 됩니다.

UPDATE scott.err_code
set id='X7202'
where rowid='AAACkVAAGAAAAAnABK';

참고포스팅 :  http://gyh214.tistory.com/81