I have a table (Oracle 11g) on which multiple packages/stored procs run DML statements. I want to capture the package/procedure name which issued the DML on the table using a trigger and log it into a logging table.

For example:

Package MY_PACK.MY_PROC() issues an insert into... for the table mytab. I would design a trigger on mytab which should be able to capture the package/procedure name that issued the insert into.. and store this info in another table my_tab_log.

I did some search and found that $$PLSQL_UNIT and $$PLSQL_LINE could point out the procedure name, but then if these variables are used from within a trigger, the trigger name would be captured instead of the package/procedure name which issued the DML statement.

like -

CREATE OR REPLACE TRIGGER my_trg
AFTER INSERT OR UPDATE OR DELETE
ON MY_TAB
FOR EACH ROW
BEGIN
IF INSERTING THEN
insert into my_tab_log values('INSERTED A ROW'
sysdate,
$$PLSQL_UNIT);
END IF;
-- This would capture Trigger name but I would like to capture `MY_PACK.MY_PROC()`
-- which issued the insert statement
...
END;

Now since $$PLSQL_UNIT is a conditional compilation directive. It is resolved when you complie/recompile PL/SQL code. So unfortunately, $$PLSQL_UNIT within trigger is nothing more but trigger name and is resolved at trigger compile time.

I also found the procedure owa_util.who_called_me but couldn't wrap my head around on how I could use this to fit my needs. Is that even possible to achieve what I want without making changes to the actual packages/stored procs which issues the DML statements? I cannot modify those programs and that is a hard restriction to it so that is not an option.

1 Answer
1

$$PLSQL_UNIT would only provide the package name, not the name of the procedure within the package. The same is true of who_called_me.

owa_util.who_called_me is based on a little utility written by the inestimable Mr Kyte. If you take a peek at his source code here you will see that the routine gets its information from the call stack. Hence the information it offers is:

program owner

program name (package, or standalone procedure)

program type

line number

These admittedly frustrating limitations are down to overloading: we can create packaged procedures with the same name but different signatures. Consequently "procedure name" is not particularly useful to the system when it comes to identifying which piece of code is operating.

Anyway, if you want to have a play with who_called_me, it takes four out parameters like this: