2011-08-15 4 views
2

다음 병합 절차가 있습니다. 예외 처리 부분의 병합 문에서 값에 액세스하는 방법은 무엇입니까?병합 문에서 값에 액세스

procedure merge_students 
is 
begin 
     merge into 
       students a 
      using 
       studentstmp t 
      on 
       (a.code = t.code) 
      when matched then update set a.name = t.name, 

      when not matched then insert (code,name) 
            values (t.code,t.name); 
      EXCEPTION 
       WHEN DUP_VAL_ON_INDEX THEN 
        dbms_output.put_line('students code: ' || a.code); //how to access a.code here 
        dbms_output.put_line('studentsTMP code: ' || t.code); // and t.code here 
end; 

답변

4

Oracle 버전에 따라 DML 오류 로깅을 사용할 수 있습니다.

의 라인을 따라 뭔가

SQL> exec dbms_errlog.create_error_log('FOO'); 

PL/SQL procedure successfully completed. 

병합 LOG 오류 구문 오류 로그 테이블 만들기

SQL> create table foo (
    2 col1 number primary key, 
    3 col2 number unique 
    4 ); 

Table created. 

SQL> create table foo_temp (
    2 col1 number, 
    3 col2 number 
    4 ); 

Table created. 

SQL> insert into foo values(1, 1); 

1 row created. 

SQL> insert into foo_temp values(2, 1); 

1 row created. 

SQL> insert into foo_temp values(3, 2); 

1 row created. 

데이터와 소스 & 대상 테이블을 만들기

하나의 행이 고유하게 제한 조건 예외를 생성하고 오류 테이블에 쓰여지는 동안 하나의 행이 성공적으로 병합 되었음에 유의하십시오.

SQL> merge into foo 
    2 using foo_temp on (foo.col1 = foo_temp.col1) 
    3 when matched then 
    4  update set foo.col2 = foo_temp.col2 
    5 when not matched then 
    6  insert(col1, col2) 
    7  values(foo_temp.col1, foo_temp.col2) 
    8 log errors into err$_foo 
    9 reject limit unlimited; 

1 row merged. 

SQL> select * from foo; 

     COL1  COL2 
---------- ---------- 
     1   1 
     3   2 

SQL> select * from foo_temp; 

     COL1  COL2 
---------- ---------- 
     2   1 
     3   2 

SQL> select * from err$_foo; 

ORA_ERR_NUMBER$ 
--------------- 
ORA_ERR_MESG$ 
-------------------------------------------------------------------------------- 

ORA_ERR_ROWID$ 
-------------------------------------------------------------------------------- 

OR 
-- 
ORA_ERR_TAG$ 
-------------------------------------------------------------------------------- 

COL1 
-------------------------------------------------------------------------------- 

COL2 
-------------------------------------------------------------------------------- 

       1 
ORA-00001: unique constraint (SCOTT.SYS_C0024443) violated 

I 

2 
1 
+0

끝내 주셔서 감사합니다. – Asterisk

+0

SQL % ... 옵션이 있으면 오류가 있는지 알 수 있습니까? 아니면 내가 잘못 $ 테이블을 쿼리해야합니까? – ShoeLace

+0

신경 쓰지 마 .. 거기 없다 .. http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/sql_cursor.htm – ShoeLace

관련 문제