I'm new to triggers, so obviously I'm doing something wrong. I am working on a report table that will receive data from source tables. For simplicity, we say that there is one table, and then there is one report table.
Original table (orig_tab)
CREATE TABLE orig_tab ( PK NUMBER(8) not null, NAME VARCHAR2(20) , ); INSERT INTO orig_tab (PK, NAME) VALUES (1, 'AAA'); INSERT INTO orig_tab (PK, NAME) VALUES (2, 'BBB'); INSERT INTO orig_tab (PK, NAME) VALUES (3, 'CCC');
Then the report table appears (rep_tab)
CREATE TABLE rep_tab ( PK NUMBER(8) not null, NAME VARCHAR2(20) , );
Now someone from the user inteface is changing the value of record 2. Obviously, this should be considered as an insert (since this record does not exist) for the report table. Then, after some time, the value changes, so it is an update case for the report table.
Question: How can I make this trigger? I suppose this is a merger case.
This is what I did:
create or replace trigger vr_reporting_trigger after update on orig_tab for each row begin MERGE INTO rep_tab d USING (SELECT pk FROM orig_tab) s ON (d.pk = s.pk) WHEN MATCHED THEN UPDATE SET d.pk = s.pk, d.name = s.name WHEN NOT MATCHED THEN INSERT (d.pk, d.name) VALUES (s.pk, s.name); end vr_reporting_trigger;
Any suggestions or recommendations that can help me figure this out? Thanks.