Oracle PL/SQL:如何获取堆栈跟踪、包名称和过程名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7032373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:28:23  来源:igfitidea点击:

Oracle PL/SQL: how to get the stack trace, package name and procedure name

oracleexception-handlingplsqlstack-trace

提问by Revious

Sometimes the exception returns something like: "ORA-06502: PL/SQL: numeric or value error: character string buffer too small".

有时异常返回类似于:“ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小”。

It's not so readable since it doesn't report neither the table, the column and the value it tried to write.

它不是那么易读,因为它既不报告表、列和它试图写入的值。

it would be useful to get the current procedure name at the moment the Exception happened or is catched.

在异常发生或被捕获时获取当前过程名称会很有用。

How can I obtain that?

我怎样才能得到它?

回答by Justin Cave

You probably want DBMS_UTILITY.FORMAT_ERROR_BACKTRACEfunction

你可能想要DBMS_UTILITY.FORMAT_ERROR_BACKTRACE功能

SQL> ed
Wrote file afiedt.buf

  1  create or replace procedure p1
  2  is
  3  begin
  4    raise_application_error( -20001, 'Error 1', true );
  5* end;
SQL> /

Procedure created.

SQL> create or replace procedure p2
  2  as
  3  begin
  4    null;
  5    p1;
  6  end;
  7  /

Procedure created.

SQL> begin
  2    p2;
  3  exception
  4    when others then
  5      dbms_output.put_line( dbms_utility.format_error_backtrace );
  6  end;
  7  /
ORA-06512: at "SCOTT.P1", line 4
ORA-06512: at "SCOTT.P2", line 5
ORA-06512: at
line 2


PL/SQL procedure successfully completed.

回答by Roeland Van Heddegem

I use the combination of DBMS_UTILITY.FORMAT_ERROR_STACKand DBMS_UTILITY.FORMAT_ERROR_BACKTRACE. (Improving the answer of Justin Cave)

我使用DBMS_UTILITY.FORMAT_ERROR_STACKDBMS_UTILITY.FORMAT_ERROR_BACKTRACE的组合。(改进Justin Cave的回答)

when others then
  Dbms_Output.put_line ( DBMS_UTILITY.FORMAT_ERROR_STACK() );
  Dbms_Output.put_line ( DBMS_UTILITY.FORMAT_ERROR_BACKTRACE() );

This gives the error on the first line and the stack on the following lines: (output from the example given by Justin Cave)

这给出了第一行的错误和以下几行的堆栈:(来自 Justin Cave 给出的示例的输出)

ORA-20001: Error 1
ORA-06512: at "SCOTT.X1", line 4
ORA-06512: at "SCOTT.X2", line 5
ORA-06512: at line 2

回答by mamboking

Or you could use DBMS_DEBUG.PRINT_BACKTRACE

或者你可以使用 DBMS_DEBUG.PRINT_BACKTRACE