Solved

The Oracle triggers

Posted on 2014-01-29
8
438 Views
Last Modified: 2014-03-02
Right now we are doing BEFORE delete/AFTER INSERT/AFTER update trigger for any Oralce DB, the MySQL one has been done:

 DROP TRIGGER IF EXISTS PACKAGE_ELEMENT_BEFORE_DELETE_TRIGGER; CREATE TRIGGER PACKAGE_ELEMENT_BEFORE_DELETE_TRIGGER BEFORE DELETE ON PACKAGE_ELEMENT FOR EACH ROW BEGIN  INSERT INTO Audit_info.PCCWSP_DBAudit_PACKAGE_ELEMENT SET  TriggerAction="BEFORE", ActionDone="DELETE", ActionDate=now(), ActionBy=USER() , PACKAGE_ID=OLD.PACKAGE_ID, ELEMENT_ID=OLD.ELEMENT_ID, PRODUCT_FAMILY=OLD.PRODUCT_FAMILY, PRODUCT_TYPE=OLD.PRODUCT_TYPE, VENDOR_TYPE=OLD.VENDOR_TYPE, VENDOR_ID=OLD.VENDOR_ID, ITEM_CATEGORY=OLD.ITEM_CATEGORY, ITEM_SUBCATEGORY=OLD.ITEM_SUBCATEGORY, NOMENCLATURE=OLD.NOMENCLATURE, FROM_COUNTRY=OLD.FROM_COUNTRY, FROM_CITY=OLD.FROM_CITY, FROM_POP_ID=OLD.FROM_POP_ID, TO_COUNTRY=OLD.TO_COUNTRY, TO_CITY=OLD.TO_CITY, TO_POP_ID=OLD.TO_POP_ID, FROM_SITE_TYPE=OLD.FROM_SITE_TYPE, TO_SITE_TYPE=OLD.TO_SITE_TYPE, FROM_BANDWIDTH=OLD.FROM_BANDWIDTH, FROM_BW_UNIT=OLD.FROM_BW_UNIT, TO_BANDWIDTH=OLD.TO_BANDWIDTH, TO_BW_UNIT=OLD.TO_BW_UNIT, PARTNERSHIP_MODEL=OLD.PARTNERSHIP_MODEL, PROTECTION=OLD.PROTECTION, RESTORABLE=OLD.RESTORABLE, CREATED_DT=OLD.CREATED_DT, CREATED_BY=OLD.CREATED_BY, UPDATED_DT=OLD.UPDATED_DT, UPDATED_BY=OLD.UPDATED_BY, AAA=OLD.AAA, AAB=OLD.AAB; END ; 

Open in new window


what should be the correct version for Oracle 10g and 11g ?
0
Comment
Question by:marrowyung
  • 3
  • 2
8 Comments
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 200 total points
ID: 39820261
in oracle you can use "create or replace"
if the trigger exists, it will replace the existing one, if not, it will create it
CREATE OR REPLACE TRIGGER PACKAGE_ELEMENT_BEFORE_DELETE_TRIGGER 
BEFORE DELETE ON PACKAGE_ELEMENT 
FOR EACH ROW 
BEGIN  
  INSERT INTO Audit_info.PCCWSP_DBAudit_PACKAGE_ELEMENT 
  SET  TriggerAction="BEFORE", ActionDone="DELETE", ActionDate=SYSDATE, 
    ActionBy=USER , PACKAGE_ID=:OLD.PACKAGE_ID, 
    ELEMENT_ID=:OLD.ELEMENT_ID, 
    PRODUCT_FAMILY=:OLD.PRODUCT_FAMILY, 
    PRODUCT_TYPE=:OLD.PRODUCT_TYPE, 
    etc ... etc ...
END ;

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39820611
In oracle you can do delete, insert and update all in one trigger.
But change the BEFORE DELETE to AFTER DELETE (it shouldn't matter)

The INSERT syntax is different in Oracle, you don't use SET

CREATE TRIGGER trg_package_element_ariud
    AFTER DELETE OR INSERT OR UPDATE
    ON package_element
    FOR EACH ROW
BEGIN
    INSERT INTO audit_info.pccwsp_dbaudit_package_element(
                    triggeraction,
                    actiondone,
                    actiondate,
                    actionby,
                    package_id,
                    element_id,
                    product_family,
                    product_type,
                    vendor_type,
                    vendor_id,
                    item_category,
                    item_subcategory,
                    nomenclature,
                    from_country,
                    from_city,
                    from_pop_id,
                    to_country,
                    to_city,
                    to_pop_id,
                    from_site_type,
                    to_site_type,
                    from_bandwidth,
                    from_bw_unit,
                    to_bandwidth,
                    to_bw_unit,
                    partnership_model,
                    protection,
                    restorable,
                    created_dt,
                    created_by,
                    updated_dt,
                    updated_by,
                    aaa,
                    aab
                )
         VALUES (
                    'AFTER',
                    CASE
                            WHEN INSERTING THEN 'INSERT'
                            WHEN UPDATING THEN 'UPDATE'
                            ELSE 'DELETE'
                        END,
                    SYSDATE,
                    USER,
                    :old.package_id,
                    :old.element_id,
                    :old.product_family,
                    :old.product_type,
                    :old.vendor_type,
                    :old.vendor_id,
                    :old.item_category,
                    :old.item_subcategory,
                    :old.nomenclature,
                    :old.from_country,
                    :old.from_city,
                    :old.from_pop_id,
                    :old.to_country,
                    :old.to_city,
                    :old.to_pop_id,
                    :old.from_site_type,
                    :old.to_site_type,
                    :old.from_bandwidth,
                    :old.from_bw_unit,
                    :old.to_bandwidth,
                    :old.to_bw_unit,
                    :old.partnership_model,
                    :old.protection,
                    :old.restorable,
                    :old.created_dt,
                    :old.created_by,
                    :old.updated_dt,
                    :old.updated_by,
                    :old.aaa,
                    :old.aab
                );
END;
0
 
LVL 1

Author Comment

by:marrowyung
ID: 39832730
sdstuber,

"But change the BEFORE DELETE to AFTER DELETE "

so there are no before delete ?

Also in MySQL, I use the following function to dynamically generate the BEFORE DELETE trigger statement:

CREATE FUNCTION `Function_Create_BEFORE_DELETE_TRIGGER` (Trigger_databasename VARCHAR(40),  trigger_tablename VARCHAR(40))  	
RETURNS TEXT
NOT DETERMINISTIC      
READS SQL DATA
BEGIN  
DECLARE a, b, fieldnameCursor_finished  INT DEFAULT 10;
DECLARE temptable VARCHAR(50);	
DECLARE today TIMESTAMP DEFAULT CURRENT_DATE;
DECLARE v1, v2, v3 TINYINT ;
DECLARE current_fieldname  VARCHAR(50) DEFAULT ""; 
/* DECLARE fullexcutecmd VARCHAR(65535);
DECLARE fullexcutecmd1 VARCHAR(5000);
DECLARE fullexcutecmd2 VARCHAR(5000);
DECLARE fullexcutecmd3 VARCHAR(60000);*/
DECLARE fullexcutecmd TEXT;
DECLARE fullexcutecmd1 TEXT;
DECLARE fullexcutecmd2 TEXT;
DECLARE fullexcutecmd3 TEXT;

/*Declare and populate the cursor with a SELECT statement */  	
	  
DECLARE fieldname CURSOR FOR 	
SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = Trigger_databasename AND TABLE_NAME = trigger_tablename ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET fieldnameCursor_finished = 1;     

  
SET @fullexcutecmd1= CONCAT( '  DROP TRIGGER IF EXISTS ',trigger_tablename,'_BEFORE_DELETE_TRIGGER; CREATE TRIGGER ',trigger_tablename,'_BEFORE_DELETE_TRIGGER BEFORE DELETE ON ',trigger_tablename,' FOR EACH ROW
BEGIN 
INSERT INTO Audit_info.',Trigger_databasename,'_DBAudit_',trigger_tablename,' SET 
TriggerAction=','"','BEFORE','",','
ActionDone=','"','DELETE','",','
ActionDate=now(),
ActionBy=USER()');   

OPEN fieldname ;	

set @fullexcutecmd2=' ';
set @fullexcutecmd3=' ';

	get_fieldlist: LOOP	
 
   FETCH fieldname INTO current_fieldname;	
         IF fieldnameCursor_finished = 1 THEN 	
          LEAVE get_fieldlist;	
          END IF;	
 
SET @fullexcutecmd2 = CONCAT(' ',current_fieldname,'=OLD.',current_fieldname);
 set @fullexcutecmd3= CONCAT(@fullexcutecmd3,',',@fullexcutecmd2); 

      END LOOP  get_fieldlist;	
close fieldname;                             
  
  set @fullexcutecmd3= CONCAT(@fullexcutecmd1,@fullexcutecmd3,'; END ;'); 

  
END $$
DELIMITER ; 

Open in new window


is there anyway for Oracle to get dynamic SQL statement to construct that AFTER/BEFORE DELETE trigger?

Can it automatically execute as MySQL can't execute that create trigger statements itself.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:marrowyung
ID: 39846588
sdstuber,

"AFTER DELETE OR INSERT OR UPDATE"

So this mean we are now doing AFTRE delete/insert/update for this trigger? but we can't just do "BFFOR DELETE OR INSERT OR UPDATE" at the same time ?
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 300 total points
ID: 39878439
>>> So this mean we are now doing AFTRE delete/insert/update for this trigger? but we can't just do "BFFOR DELETE OR INSERT OR UPDATE" at the same time ?

no, a trigger is either BEFORE or AFTER an event.

Based on your examples and descriptions above it makes the most sense to just use a single AFTER trigger.

This avoids complications with statement restart or rollbacks for mid statement failures.
0
 
LVL 1

Author Comment

by:marrowyung
ID: 39898663
"no, a trigger is either BEFORE or AFTER an event."

you just mean the trigger BELETE, INSERT and after can conbine in one trigger statement but just BEFORE or AFTER, right?
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can i Import Access Table Into Oracle Using Toad 36 179
Foxpro errors 4 34
Oracle encryption 12 34
Mongo DB 18 46
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question