在 Oracle PL/SQL 中获取调用过程或函数的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7273982/
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:30:19  来源:igfitidea点击:

Get the name of the calling procedure or function in Oracle PL/SQL

oraclefunctionplsqlprocedure

提问by Paul Yeoman

Does anyone know whether it's possible for a PL/SQL procedure (an error-logging one in this case) to get the name of the function/procedure which called it?

有谁知道 PL/SQL 过程(在这种情况下是错误记录过程)是否有可能获得调用它的函数/过程的名称?

Obviously I could pass the name in as a parameter, but it'd be nice to make a system call or something to get the info - it could just return null or something if it wasn't called from a procedure/function.

显然,我可以将名称作为参数传入,但最好进行系统调用或其他方式来获取信息 - 如果不是从过程/函数中调用,它可以只返回 null 或其他内容。

If there's no method for this that's fine - just curious if it's possible (searches yield nothing).

如果没有这方面的方法,那很好 - 只是好奇是否有可能(搜索一无所获)。

采纳答案by APC

There is a package called OWA_UTIL(which is not installed by default in older versions of the database). This has a method WHO_CALLED_ME()which returns the OWNER, OBJECT_NAME, LINE_NO and CALLER_TYPE. Note that if the caller is a packaged procedure it will return the PACKAGE name not the procedure name. In this case there is no way of getting the procedure name; this is because the procedure name can be overloaded, so it's not necessarily very useful.

有一个名为OWA_UTIL(默认情况下未安装在旧版本数据库中)的包。这有一个WHO_CALLED_ME()返回 OWNER、OBJECT_NAME、LINE_NO 和 CALLER_TYPE 的方法。请注意,如果调用方是一个打包过程,它将返回 PACKAGE 名称而不是过程名称。在这种情况下,无法获得过程名称;这是因为过程名称可以重载,所以它不一定很有用。

Find out more.

了解更多



Since 10gR2 there is also the $$PLSQL_UNITspecial function; this will also return the OBJECT NAME (i.e. package not packaged procedure).

从 10gR2 开始也有$$PLSQL_UNIT特殊功能;这也将返回对象名称(即包未打包的程序)。

回答by CristiC

I found this forum: http://www.orafaq.com/forum/t/60583/0/. It may be what you are looking.

我找到了这个论坛:http: //www.orafaq.com/forum/t/60583/0/。这可能就是你正在寻找的。

Basically, you can use the Oracle supplied dbms_utility.format_call_stack:

基本上,您可以使用 Oracle 提供的dbms_utility.format_call_stack

scott@ORA92> CREATE TABLE error_tab
  2    (who_am_i      VARCHAR2(61),
  3     who_called_me VARCHAR2(61),
  4     call_stack    CLOB)
  5  /

Table created.

scott@ORA92> 
scott@ORA92> CREATE OR REPLACE PROCEDURE d
  2  AS
  3    v_num      NUMBER;
  4    v_owner    VARCHAR2(30);
  5    v_name     VARCHAR2(30);
  6    v_line     NUMBER;
  7    v_caller_t VARCHAR2(100);
  8  BEGIN
  9    select to_number('a') into v_num from dual; -- cause error for testing
 10  EXCEPTION
 11    WHEN OTHERS THEN
 12      who_called_me (v_owner, v_name, v_line, v_caller_t);
 13      INSERT INTO error_tab
 14      VALUES (who_am_i,
 15          v_owner || '.' || v_name,
 16          dbms_utility.format_call_stack);
 17  END d;
 18  /

Procedure created.

scott@ORA92> SHOW ERRORS
No errors.
scott@ORA92> CREATE OR REPLACE PROCEDURE c
  2  AS
  3  BEGIN
  4    d;
  5  END c;
  6  /

Procedure created.

scott@ORA92> CREATE OR REPLACE PROCEDURE b
  2  AS
  3  BEGIN
  4    c;
  5  END b;
  6  /

Procedure created.

scott@ORA92> CREATE OR REPLACE PROCEDURE a
  2  AS
  3  BEGIN
  4    b;
  5  END a;
  6  /

Procedure created.

scott@ORA92> execute a

PL/SQL procedure successfully completed.

scott@ORA92> COLUMN who_am_i FORMAT A13
scott@ORA92> COLUMN who_called_me FORMAT A13
scott@ORA92> COLUMN call_stack    FORMAT A45
scott@ORA92> SELECT * FROM error_tab
  2  /

WHO_AM_I      WHO_CALLED_ME CALL_STACK
------------- ------------- ---------------------------------------------
SCOTT.D       SCOTT.C       ----- PL/SQL Call Stack -----
                              object      line  object
                              handle    number  name
                            6623F488         1  anonymous block
                            66292138        13  procedure SCOTT.D
                            66299430         4  procedure SCOTT.C
                            6623D2F8         4  procedure SCOTT.B
                            6624F994         4  procedure SCOTT.A
                            66299984         1  anonymous block


scott@ORA92>