Pages

Thursday, February 13, 2014

Triggers in Oracle by Example

=============================

How to see on which table there are triggers?
=============================


SELECT owner, trigger_name, triggering_event, trigger_body
 FROM DBA_TRIGGERS 
WHERE table_name  like '%MY_TABLE%';

=============================
How to see trigger body?
=============================


Trigger body defined as LONG datatype in USER_TRIGGERS.

SET LONG 100000
SPOOL terigger_text.sql
SELECT description, TRIGGER_BODY  
 FROM USER_TRIGGERS 
WHERE trigger_name  like '%LSM_TEXT%';
SPOOL OFF

=============================
How to see all triggers on a Table
=============================
SELECT * 
FROM  ALL_SOURCE 
WHERE type = 'TRIGGER' 
  AND text like '%MY_TABLE%';

=============================
Example of adding a trigger for date Timestamp.
=============================
CREATE OR REPLACE TRIGGER MY_TABLE_TG
  BEFORE UPDATE OR INSERT ON MY_TABLE
  FOR EACH ROW
BEGIN
   :NEW.upd_date := SYSDATE;
END;




No comments:

Post a Comment