oracle - 访问合并语句中的值

标签 oracle exception 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;

最佳答案

根据 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/

相关文章:

oracle - 连接到 oracle 表值函数

c# - 从 Called 函数抛出异常到 Caller Function 的 Catch block

exception - Gradle 构建/测试失败 - kryo.KryoException : Buffer overflow

git - 如何识别需要在 git 中 merge 的分支

r - 使用 data.table 进行左连接

git - 错误 merge 后修复 Git 仓库

java - 使用 EclipseLink JPA 审计 Oracle

c# - 通过c#将整数数组传递给oracle过程

java - HTTP 状态 500 - servlet appServlet 的 Servlet.init() 在 spring MVC 中抛出异常

Python,模拟 : raise exception