我有以下合并过程。如何从异常处理部分中的合并语句访问值...
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;
最佳答案
根据 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 语法进行合并
请注意,一行已成功合并,同时一行生成了唯一约束异常并被写入错误表。
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
关于oracle - 访问合并语句中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061989/