Currently Executing Procedure Name within the Package

后端 未结 4 419
囚心锁ツ
囚心锁ツ 2020-12-18 06:32

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;

cre         


        
4条回答
  •  情歌与酒
    2020-12-18 07:10

    An approach using the idea borrowed from here : http://www.orafaq.com/forum/t/173023/

    Note : This works fine only in Oracle 12c. For 11g, It only gives Package name.

    Create a procedure called show_caller. This uses the OWA_UTIL.WHO_CALLED_ME procedure.

    create or replace procedure show_caller
    as
       l_owner  varchar2(200);
       l_name   varchar2(200); 
       l_lineno number;
       l_caller varchar2(200);
    begin 
       OWA_UTIL.WHO_CALLED_ME (l_owner, l_name,l_lineno,l_caller);
       dbms_output.put_line('Inside '||l_name);
    
    end;
    /
    

    Now, you can write your package as:

    CREATE OR replace PACKAGE test_pkg 
    AS 
      PROCEDURE proc1; 
    END test_pkg; 
    
    / 
    CREATE OR replace PACKAGE BODY test_pkg 
    AS 
      PROCEDURE Proc1 
      AS 
      BEGIN 
          show_caller;  -- proc1 calls show_caller 
      END proc1; 
    END test_pkg; 
    
    / 
    

    Execution.

    SET SERVEROUTPUT ON
    BEGIN
       Test_pkg.Proc1;
    END;
    /
    Inside TEST_PKG.PROC1
    

    Note that this will print the name of the procedure.If you want to use it as a variable, pass l_name(along with others if needed) as OUT variable from show_caller

    Live SQL Demo ( Free OTN account required )

    One other option is to use OWA_UTIL.GET_PROCEDURE function within the procedure: but it doesn't seem work for me in this context. I would be glad to know more about this from experts.

提交回复
热议问题