记录oracle中的行号

时间:2015-09-14 14:40:23

标签: oracle plsql error-handling exception-handling

我目前正在处理过程中的日志错误。此过程的目标是在DB中的其他包中的异常处理程序中调用,并记录每个程序遇到的错误。下面是我的代码。

CREATE OR REPLACE PROCEDURE APMS.test_procedure AS

    procedure write_error_log (errcode number, errstr varchar2) is
    pragma autonomous_transaction; 
    -- this procedure stays in its own new private transaction
    begin                         
          INSERT INTO error_log
               (ora_err_tmsp,
                      ora_err_number,
                      ora_err_msg,
                      ora_err_line_no)
          values (CURRENT_TIMESTAMP,
                  errcode,
                  errstr,
                  'line number');
          COMMIT;  -- this commit does not interfere with the caller's transaction.
    end write_error_log;

 BEGIN
    INSERT INTO mockdata
        VALUES ('data1', 'mockname', 'mockcity'); 

  exception when others then             
    write_error_log(sqlcode,sqlerrm);
    raise; 
 END test_procedure;
/

我目前只是在我的mock_data表中引发错误,以便在error_log表中记录错误,看看它的功能是否只是无法弄清楚如何记录行号列。我是一个完全的初学者,所以任何帮助都会赞赏。 Addiotionally,如果有人知道我如何能够在其他包/过程中使用此过程来记录其他包中的错误,这也是很棒的。我在这里学习,所以任何反馈都表示赞赏,如果我不清楚,我可以进一步扩展这篇文章。

2 个答案:

答案 0 :(得分:2)

尝试使用DBMS_UTILITY.FORMAT_ERROR_BACKTRACE。您可以查看here以获取更多信息。

这样的事情应该使你的代码成为可能:

CREATE OR REPLACE PROCEDURE APMS.test_procedure AS

    procedure write_error_log (errcode number, errstr varchar2,errline varchar2) is
    pragma autonomous_transaction; 
    -- this procedure stays in its own new private transaction
    begin                         
          INSERT INTO error_log
               (ora_err_tmsp,
                      ora_err_number,
                      ora_err_msg,
                      ora_err_line_no)
          values (CURRENT_TIMESTAMP,
                  errcode,
                  errstr,
                  errline);
          COMMIT;  -- this commit does not interfere with the caller's transaction.
    end write_error_log;

 BEGIN
    INSERT INTO mockdata
        VALUES ('data1', 'mockname', 'mockcity'); 

  exception when others then             
    write_error_log(sqlcode,sqlerrm,DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
    raise; 
 END test_procedure;

答案 1 :(得分:1)

使用以下命令获取调用堆栈:

  • dbms_utility.format_error_stack
  • dbms_utility.format_error_backtrace

例如,

SQL> declare
  2    v1 integer := 1;
  3    v2 integer := 0;
  4    v3 integer;
  5    procedure p1 (v1 in integer, v2 in integer, v3 out integer) is 
  6    begin 
  7      v3 := v1 / v2;
  8    end;
  9    procedure p2 (v1 in integer, v2 in integer, v3 out integer) is 
 10    begin 
 11      p1 (v1, v2, v3);
 12    end;
 13  begin
 14    p2 (v1, v2, v3);
 15  exception
 16    when others then
 17      dbms_output.put_line ('---------------------');
 18      dbms_output.put_line ('This is what you record in log table:');
 19      dbms_output.put (dbms_utility.format_error_stack);
 20      dbms_output.put (dbms_utility.format_error_backtrace);
 21      dbms_output.put_line ('---------------------');
 22      raise;
 23  end;
 24  /
---------------------
This is what you record in log table:
ORA-01476: divisor is equal to zero
ORA-06512: at line 7
ORA-06512: at line 11
ORA-06512: at line 14
---------------------
declare
*
ERROR at line 1:
ORA-01476: divisor is equal to zero
ORA-06512: at line 22