Skip to Main Content
  • Questions
  • Name of the table or name the trigger in a body of the trigger

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Sergej.

Asked: November 14, 2007 - 12:10 pm UTC

Last updated: November 14, 2007 - 4:59 pm UTC

Version: 10.2.0

Viewed 1000+ times

You Asked

How can I get a name of the table to which this trigger belongs or a name of the trigger in a body of the trigger?
Thanks in advance.

and Tom said...

ops$tkyte%ORA10GR2> create table t ( x int );
11

Table created.

ops$tkyte%ORA10GR2> create or replace trigger t_trigger
  2  before insert on t
  3  begin
  4          dbms_output.put_line( dbms_utility.format_call_stack );
  5  end;
  6  /
12

Trigger created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> insert into t values ( 1 ) ;
----- PL/SQL Call Stack -----
  object      line  object
  handle    number
name
0x368acb0c         2  OPS$TKYTE.T_TRIGGER


1 row created.


the bottom of the call stack will be your trigger name...

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Tom Fox, November 15, 2007 - 7:45 pm UTC

And DBA_TRIGGERS has a column named TABLE_NAME and OWNER which will show you the table name and owner to which the trigger belongs.

wouldn't it be nice ...

Sokrates, November 16, 2007 - 6:01 am UTC

... if we would have a
dbms_utility.call_stack
returning some data structure
to access the real call stack ?

(and not access the *formatted* call stack and have to parse it to get the call stack out of the format)

Cut the stack

Dmytro Dekhtyaryuk, November 16, 2007 - 6:33 am UTC

Nice idea !

... And if we want to get only trigger name,
we can cut the message and then really search in Data Dictionary (all_triggers) for table name ...

Something like

SQL> set serveroutput on
SQL> 
SQL> create or replace trigger t_trigger
  2      before insert on t
  3      declare
  4      v_trig_name VARCHAR2(30);
  5      v_table_name VARCHAR2(30);
  6      begin
  7              v_trig_name := rtrim(substr(dbms_utility.format_call_stack,
  8                                   INSTR(dbms_utility.format_call_stack,'.',-1)+1),chr(10));
  9              SELECT table_name INTO v_table_name
 10              FROM all_triggers
 11              WHERE trigger_name = v_trig_name;
 12              dbms_output.put_line( ' Trigger:'||v_trig_name||
 13                                    ' Table:'||v_table_name);
 14      end;
 15  /
 
Trigger created
 
SQL> insert into t values (2);
 
 Trigger:T_TRIGGER Table:T
 
1 row inserted
 
SQL>  

(Well, also owner check for SELECT ...)

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library