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

发布于 2024-12-02 16:02:12 字数 177 浏览 1 评论 0原文

有谁知道 PL/SQL 过程(在本例中是错误记录过程)是否可以获取调用它的函数/过程的名称?

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

如果没有方法,那也没关系 - 只是好奇是否可能(搜索不会产生任何结果)。

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?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栩栩如生 2024-12-09 16:02:12

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

了解更多信息


从 10gR2 开始,还有 $$PLSQL_UNIT 特殊函数;这也将返回对象名称(即包而不是打包过程)。

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.

Find out more.


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

时光是把杀猪刀 2024-12-09 16:02:12

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

基本上,您可以使用 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>

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

Basically, you can use the Oracle supplied 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>
油饼 2024-12-09 16:02:12

基本上,您需要做的就是定义变量并将它们传递到对实用程序方法的调用中,以用值填充它们:

create or replace procedure some_test_proc (p_some_int int) 
is
    owner_name VARCHAR2 (100);
    caller_name VARCHAR2 (100);
    line_number NUMBER;
    caller_type VARCHAR2 (100);
begin
    ....
    OWA_UTIL.WHO_CALLED_ME (owner_name,caller_name,line_number,caller_type);
    -- now you can insert those values along with systimestamp into a log file
    ....
end;

Basically, all you need to do is to define vars and pass them in a call to a utility method to fill them up with values:

create or replace procedure some_test_proc (p_some_int int) 
is
    owner_name VARCHAR2 (100);
    caller_name VARCHAR2 (100);
    line_number NUMBER;
    caller_type VARCHAR2 (100);
begin
    ....
    OWA_UTIL.WHO_CALLED_ME (owner_name,caller_name,line_number,caller_type);
    -- now you can insert those values along with systimestamp into a log file
    ....
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文