Find out name of PL/SQL procedure

后端 未结 4 1449
广开言路
广开言路 2021-02-04 03:36

Can PL/SQL procedure in Oracle know it\'s own name?

Let me explain:

CREATE OR REPLACE procedure some_procedure is
    v_procedure_name varchar2(32);
begi         


        
相关标签:
4条回答
  • 2021-02-04 04:26

    Try:

    v_procedure_name := $$PLSQL_UNIT;
    

    There's also $$PLSQL_LINE if you want to know which line number you are on.

    0 讨论(0)
  • 2021-02-04 04:36

    If you are pre-10g, you can 'dig' (parse) it out of dbms_utility.format_call_stack Procedures/functions in packages can be overloaded (and nested), so the package name/line number is normally better than the name.

    0 讨论(0)
  • 2021-02-04 04:36

    Here's a neat function that takes advantage of REGEXP_SUBSTR. I've tested it in a package (and it even works if another procedure in the package calls it):

    FUNCTION SET_PROC RETURN VARCHAR2 IS
    BEGIN
      RETURN NVL(REGEXP_SUBSTR(DBMS_UTILITY.FORMAT_CALL_STACK, 
                 'procedure.+\.(.+)\s', 1,1,'i',1), 'UNDEFINED');
    END SET_PROC;
    
    0 讨论(0)
  • 2021-02-04 04:37

    In 10g and 11g I use the "owa_util.get_procedure" function. I normally use this in packages as it will also return the name of an internal procedure or function as part of the package name, i.e. (package_name).(procedure name). I use this to provide a generic EXCEPTION template for identifying where an exception occured.

    CREATE OR REPLACE procedure some_procedure is
        v_procedure_name varchar2(32);
    begin
        v_procedure_name := owa_util.get_procedure;
    end;
    
    CREATE OR REPLACE PACKAGE some_package
    AS
        FUNCTION v_function_name
        RETURN DATE;
    END;
    /
    CREATE OR REPLACE PACKAGE BODY some_package
    AS
        FUNCTION v_function_name
        RETURN DATE
        IS
        BEGIN
            RETURN SYSDATE;
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('ERROR IN '||owa_util.get_procedure);
                DBMS_OUTPUT.PUT_LINE(SQLERRM);
        END;
    END;
    /
    
    0 讨论(0)
提交回复
热议问题