Hello I am trying to create a history table.. My code pretty much works fine but my reports' queries compute for the summation of rows with the same column value (x_id)
Now I would like to create a trigger that would first DELETE the old rows (For instance, row 1's value is (5x,100))
Then my trigger would insert (5x, 200), (5x, 300)
I would like to delete first the (5x, 100) column before inserting my (5x, 200) and (5x, 300)
I tried putting a DELETE statement before my INSERT in my trigger but I end up with just 1 row inserted on my history table, it deletes also the newly added rows except for the last row..
If it matters, my trigger is AFTER UPDATE and DELETE.
Also, table1 and table1_hist have the exact same column definition.
CREATE TRIGGER trig_table1_hist
ON table1
AFTER UPDATE, DELETE
AS
DELETE FROM table1_hist
WHERE table1_hist.t_id IN (SELECT t_id FROM inserted)
INSERT INTO table1_hist
SELECT *
FROM inserted
I tried this but it deletes the newly added rows too with the same primary key. Now If i don't delete, my table would then have (5x,100) (5x,200) (5x,300) But I need the (5x,100) to be disappeared because my reports would still include the (5x,100)