Jason Bennett's Developer Corner

 






Click to see the XML version of this web page.

>


View David Jason Bennett's profile on LinkedIn

 

 

A Little About Jason Bennett ...

I've had an interest in publishing technical articles and HELPFUL code for a few years.  I am (by trade and hobby) a developer who specializes in Oracle technologies and web based architectures.  I have been an employee of both TUSC and Oracle Corporation.  My intent here is to share my ideas and coding experiences with the developer community as a whole.  As with all developers some of my ideas are great and some of them are ....  well you know.  Anyway, I hope you find something here that will aid in your endeavor, or spark a new idea. 

I am more than happy to assist with technical issues and will even write a little code if need be. If you find something on the site that is really useful and you'd like to make a contribution (absolutely up to you and absolutely not required), just click the "Make a Donation" button on the left!

Good luck and good coding !




  Sunday, May 23, 2004


PL/SQL: Using MERGE to Synchronize Data from a Table Trigger

The following script demonstrates how the MERGE statement can be utilized in a table
trigger to enable the synchronization of data between two similar tables.

 


CREATE TABLE test_trigger1(id number (12),
                           col1  varchar2(50),
                           col2  varchar2(50));

 

CREATE TABLE test_trigger2(id number (12),
                           col1  varchar2(50),
                           col2  varchar2(50));

 

CREATE OR REPLACE TRIGGER l_update_test
 BEFORE UPDATE OR INSERT
 ON test_trigger1
 FOR EACH ROW
BEGIN


   MERGE INTO test_trigger2 a
   USING(SELECT :new.id id,:new.col1 col1,:new.col2 col2 from dual) b
   ON (a.id = b.id)
   WHEN MATCHED THEN
     UPDATE SET
     a.col1 = b.col1,
     a.col2 = b.col2
   WHEN NOT MATCHED THEN
     INSERT(id,col1,col2) VALUES (b.id,b.col1,b.col2);
     

END;
/

INSERT INTO test_trigger1 VALUES (1,'Test1','Test2');
INSERT INTO test_trigger1 VALUES (2,'Test3','Test4');

COMMIT;

SELECT * FROM test_trigger2;

UPDATE test_trigger1 SET col1 = 'This worked' WHERE id=2;
COMMIT;

SELECT * FROM test_trigger2;

 


 


9:21:07 AM    

Click here to visit the Radio UserLand website. © Copyright 2008Jason Bennett.
Last update: 8/28/2008; 9:44:06 PM.

May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Aug