I have the following trigger on my table, issue I am facing is in my_hist table both old and new values are getting inserted when condition becomes updated = 'Y'.
How can I efficiently insert old values into my_hist table when condition becomes updated = 'Y'.
Thanks
CREATE OR REPLACE TRIGGER mytrig
AFTER UPDATE
ON mytab
FOR EACH ROW
WHEN (
new.updated = 'Y'
)
BEGIN
INSERT INTO my_hist
VALUES (
:old.id,
:old.no,
:old.start_date,
:old.end_date,
SYSDATE
);
END mytrig;
/
Update 1
If I am updating updated = 'Y' then if I have records
id = 3, start_date=’01-Jan-2014′ and end_date=’31-Jan-2014′ and updated data
id = 3, start_date=='01-Jan-2014' and end_date='31-Mar-2014' then I will have two records in my_hist table are
id = 3 and start_date='01-Jan-2014' and end_date='31-Jan-2014'
and
id = 3 and start_date='01-Jan-2014' and end_date='31-Mar-2014'
Ideally I should have only id = 3 and start_date='01-Jan-2014' and end_date='31-Jan-2014' in my_hist table, not the second row with end_date='31-Mar-2014' because that is new data.
Something in your description is missing. If you do what you say you’re doing, your history table would have only one row and it would have the old data. If you also have a history row that has the new data, either your trigger is doing something other than what you posted or you have something else (i.e. another trigger) that is writing to the history table.
Create the two tables
Populate the initial data
Create the trigger
Now, when I update the row
there will be only one row in the
MY_HISTtable and that row will have the old values from the row inMY_TABIf you see two rows, something else is writing the second row. My guess is that you have another trigger defined.