我有以下合并程序。如何在异常处理部分中访问merge语句中的值...
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;
答案 0 :(得分:4)
根据Oracle版本,您可以使用DML错误日志记录。
的内容创建来源&包含数据的目标表
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> exec dbms_errlog.create_error_log( 'FOO' );
PL/SQL procedure successfully completed.
带有LOG ERRORS语法的MERGE
请注意,一行已成功合并,而一行生成了唯一约束异常,并被写入错误表。
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