包内当前正在执行的过程名称 [英] Currently Executing Procedure Name within the Package
问题描述
有没有办法获取包内当前正在执行的过程名称?
Is there a way to get the currently executing procedure name within the package?
create or replace package test_pkg
as
procedure proc1;
end test_pkg;
create or replace package body test_pkg
as
procedure proc1
is
-- // Get the Procedure Name here?? //
end proc1;
end test_pkg;
推荐答案
在12c中,当前子程序名只是:
In 12c, the current subprogram name is just:
utl_call_stack.subprogram(1)(2);
当前包也可以从
utl_call_stack.subprogram(1)(1);
但通常只使用 $$plsql_unit
更容易.您还可以获取限定名称 (package.procedure
) 为:
but it is generally easier to just use $$plsql_unit
. You can also get the qualified name (package.procedure
) as:
utl_call_stack.concatenate_subprogram(utl_call_stack.subprogram(1));
然而,我想不出任何情况下过程或函数(或对象方法)需要自己的名字.此功能在日志记录过程中最有用,在这种情况下,'谁打电话给我? 代码应该在记录器中,而不是在调用它的每件事中都重复.因此,我强烈建议在程序中避免任何'我是谁? 逻辑.相反,在您的记录器中放入类似的内容(需要 12.1 或更高版本):
However, I can't think of any situation where a procedure or function (or object method) would want its own name. This functionality is mostly useful in a logging procedure, in which case the 'who called me?' code should be in the logger, and not repeated in every single thing that calls it. Therefore, I would strongly suggest avoiding any 'who am I?' logic in procedures. Instead, put something like this in your logger (requires 12.1 or later):
create or replace procedure logdemo
as
k_calling_package constant varchar2(128) := utl_call_stack.subprogram(2)(1);
k_calling_subprog constant varchar2(128) := utl_call_stack.subprogram(2)(2);
begin
dbms_output.put_line
( $$plsql_unit ||
' called from package '||k_calling_package||', subprogram '||k_calling_subprog );
end logdemo;
不幸的是,它在 11g 中稍微复杂一些,因为您必须解析 dbms_utility.format_call_stack
,并且因为这只会给您包名称和行号(在换行分隔的文本字符串中),您然后必须查询all_source
以找到子程序名称.
Unfortunately it's a little more complicated in 11g as you have to parse dbms_utility.format_call_stack
, and as this only gives you the package name and line number (in a linefeed-delimited text string), you then have to query all_source
to find the subprogram name.
如果你澄清它的用途,我可以发布一些 11g 代码.在我的 11g 记录器中,我发现根据 sqlcode
等捕获 dbms_utility.format_error_backtrace
以及 dbms_utility.format_call_stack
很有用,所以有一堆特定于日志记录的逻辑,如果您只是出于其他原因想捕获当前过程名称,则可能不需要这些逻辑.
I can post some 11g code if you clarify what it's for. In my 11g logger I found it useful to capture dbms_utility.format_error_backtrace
as well as dbms_utility.format_call_stack
depending on sqlcode
etc, so there is a bunch of logic that is specific to logging, which you may not need if you just want to capture the current procedure name for some other reason.
这篇关于包内当前正在执行的过程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!