I want to create a before delete trigger. When I delete a record from a table that record has to be inserted into a history table. How can I do this in SQL Server?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In this situation, you’re probably better off doing a regular “after” trigger. This is the most common approach to this type of situation.
Something like
What will happen is, when a record (or records!) are deleted from your table, the deleted row will be inserted into
my_audit_tableTheDELETEDtable is a virtual table that contains the record(s) as they were immediately prior to the delete.Also, note that the trigger runs as part of the implicit transaction on the delete statement, so if your delete fails and rolls back, the trigger will also rollback.