I’m using sql server 2005 and have 2 tables in my database.
How can I write a trigger that can INSERT a record into table 2 BEFORE delete in table 1?
How can I do this?
I need this for VB.Net.
I’ve already done a connection with Sql server.
I’m using sql server 2005 and have 2 tables in my database. How can
Share
Logically, whether the INSERT is before or after the DELETE does not matter because the trigger is part of the implicit transaction created by the DELETE.
So if the INSERT fails, the DELETE is rolled back (with appropriate handling). INSERT first, them DELETE would the same.
In other words, you can use INSTEAD OF (or BEFORE) triggers to do the INSERT but you then have to write your own DELETE in the trigger. Because of what I said above about transactions, you can use an AFTER/FOR trigger. Something like Pankaj Agarwal’s answer