I had a need to mirror any changes to one Sql Server table to another Sql Server table of a different name. Read on.
- February 13, 2016
I had a need to mirror any changes to one Sql Server table to another Sql Server table of a different name. Both tables had similar columns and types. In this sample my table is called Dave_Test and my mirror table was Dave_Test_Audit.
I will provide the create table structures in the event you want to replicate my code.
Here are my insert, update and delete triggers.
So why does my Update trigger deal with inserts? The master table (Dave_Test) already had rows existing before the triggers were created. With the update trigger managing inserting and updating records if the record is updated and does yet belong in the master audit table then the recently updated row will be inserted into the audit table for us.
For those old enough – this is Trigger (the smartest horse in the movies)