oracle ora-01722更新语句中的无效数字where子句。我想在错误日志表中记录此错误

时间:2013-10-23 04:51:05

标签: oracle

oracle ora-01722更新语句中的无效数字where子句。 我想在错误日志表中记录此错误。我怎么能登录? 下面是我的Update语句,这里的empid是数字。

UPDATE SARVESH_TEST_ERR_TB
SET    empid = empid
WHERE  empid ='aaa'
LOG ERRORS INTO SARVESH_ERR_LOGS_TB ('Error while UPDATE') 
REJECT LIMIT UNLIMITED;

1 个答案:

答案 0 :(得分:2)

您不能(错误不会被记录)使用DML语句的错误日志记录子句来记录在DML语句的WHERE子句中引发的错误。基本上,包含update子句的DML(例如你的where语句)由两部分组成,即写入部分和读取部分 - 所以where子句是读取部分(基本上是一个选择/游标)并且您可以使用错误日志记录子句来捕获DML语句的写入部分引发的错误。为了能够捕获该错误并将其写入错误记录表,您可以使用after servererror触发器,但是您无法抑制该错误:

/* test table */
SQL> create table tb_erp(
  2    col number
  3  )
  4  /
Table created

/* simple error logging table */
SQL> create table error_logs(
  2    msg varchar2(123)
  3  )
  4  /
Table created

SQL> insert into tb_erp(col) values(1);
1 row inserted

SQL> commit;
Commit complete

SQL> create or replace trigger TR_CATCH_ERRORS
  2  after servererror on schema
  3  begin
  4    insert into error_logs(msg)
  5      values(ora_server_error_msg(1));
  6  end;
  7  /
Trigger created

测试用例:

SQL> update tb_erp t 
        set t.col = 5 
      where t.col = 'aaa';

update tb_erp t set t.col = 5 where t.col = 'aaa'
ORA-01722: invalid number

/* view errors */
SQL> select * 
  2    from error_logs;

MSG
-------------------------------
ORA-01722: invalid number

编辑#1

  

但是我必须记录错误信息,错误值('aaa'),创建日期/   错误日期,是否为insert / update语句结果集   Ora-01722:无效数字'aaa'23-oct-2013'插入/更新例外'   。有没有这样的选择

对于此特定错误,您将无法提取实际值('aaa'),但您可以执行的操作是记录导致错误的整个DML语句:

触发器的

When子句允许您列出触发器触发的错误,否则会触发任何引发的错误。

SQL> alter table error_logs add ( text     varchar2(4000)
  2                             , timestmp timestamp )
  3  ;
Table altered


create or replace trigger TR_CATCH_ERRORS
after servererror on schema
--when ( ora_server_error(1) in ('1722') )
declare
  l_sql_txt_list ora_name_list_t;
  l_elements     binary_integer;
  l_sql_txt      varchar2(4000);

begin
  /* if statement triggering an error is long
    it'll be broken into several pieces and
    in order to get a complete statamet we have to assemble 
    those pieces */
  l_elements := ora_sql_txt(l_sql_txt_list);

  for i in 1..l_elements
  loop
    l_sql_txt := l_sql_txt || l_sql_txt_list(i);
  end loop;
  insert into error_logs(msg, text, timestmp)
    values( ora_server_error_msg(1)
           , l_sql_txt
           , systimestamp );
end; 

测试用例:

SQL> update tb_erp t set t.col = 5 where t.col = 'aaa';
update tb_erp t set t.col = 5 where t.col = 'aaa'
ORA-01722: invalid number

/* view errors */

SQL> select * from error_logs;

 MSG                        TEXT                     TIMESTMP
 ---------------------------------------------------------------------------
 ORA-01722: invalid number  update tb_erp t     23-OCT-13 11.04.57.535000 AM
                               set t.col = 5 
                             where t.col = 'aaa'