Madam Zu Zu
Madam Zu Zu

Reputation: 6605

Sql Trigger to insert updated record into another table

i've been looking though different tutorials online,but i can't seem to find what i need.

I need to copy a record into a history table, every time it is updated.

Is there a way to do it with triggers without having to type out all of my data fields?

Upvotes: 1

Views: 3550

Answers (1)

AndyG
AndyG

Reputation: 41100

It would help if you posted a schema of your tables, and what exactly you want to be inserting into your history table, but for now I'll make some assumptions about the table you're updating, and what you want in your history table. As an aside, this trigger will not work for a copy-paste... as each subsequent update to a record will be unable to be inserted into your history table because of primary key violations.

CREATE TRIGGER trg_History AFTER UPDATE ON my_table
FOR EACH ROW INSERT INTO history_table VALUES (NEW.col1, NEW.col2, OLD.col1, OLD.col2 ... etc)

the NEW keyword refers to all the data being inserted/updated and OLD refers to, well, the old data before being overwritten.

Again, please be more specific with the information you need to be inserting, and what you've tried so far, as we can only help you with general syntax at this point in time.

Upvotes: 1

Related Questions