Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of the calling procedure or function in Oracle PL/SQL

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).

like image 276
Paul Yeoman Avatar asked Sep 01 '11 17:09

Paul Yeoman


People also ask

How to call a function from a PL/SQL procedure?

Answer: Calling Procedures from PL/SQL is easy, and there are several methods for invoking a function: 1 - Call an independent function from inside SQL from dual. 2 - Call the function from SQL using a table. 3 - Call a function within an assignment operator. 4 - Call a PL/SQL function from inside an "IF" Boolean expression.

How do I call a function in Oracle SQL Developer?

To compile the function in Oracle SQL Developer, you click the Run Statement button as shown in the picture below: Once the function is compiled successfully, you can find it under the Functions node: Calling a PL/SQL function You use a function anywhere that you use an expression of the same type.

What is a function in Oracle?

A function is a subprogram stored in an Oracle database that returns a value. It is a stored PL/SQL block just like procedures but there is a difference. A function always returns a value whereas a procedure may or may not return a value.

Can you call a function from a table name in SQL?

You don't always have to use the DUAL pseudotable to call a function, you can also pass a table name to the function: In a PL/SQL block, a function can be called in a SQL statement (as seen above) or used in a simple assignment operation. Functions can also be used to validate variables.


2 Answers

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).

like image 89
APC Avatar answered Oct 20 '22 10:10

APC


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>
like image 20
CristiC Avatar answered Oct 20 '22 11:10

CristiC