Oracle中软件包中当前正在执行的过程名称 [英] Currently Executing Procedure Name within the Package in Oracle

查看:359
本文介绍了Oracle中软件包中当前正在执行的过程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Oracle软件包中获取当前正在执行的过程名称?

Is there a way to get the currently executing procedure name within the package in Oracle?

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;

不幸的是,由于必须解析dbms_utility.format_call_stack,因此在11g中它要复杂一些,并且由于它仅给您包名称和行号(在以换行分隔的文本字符串中),因此您必须查询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记录器中,我发现捕获dbms_utility.format_error_backtracedbms_utility.format_call_stack(取决于sqlcode等)非常有用,因此有很多特定于日志记录的逻辑,如果只想捕获,则可能不需要出于其他原因,当前的过程名称.

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.

这篇关于Oracle中软件包中当前正在执行的过程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆