Sql Server 2005 Transactions For Deployment Scripts
I want to wrap my SQL deployment script in a transaction (containing a bunch of schema changes). I am doing this because if one part of it fails, I want the db to revert to wha开发者_如何学运维t it was before I ran the script.
I have a few simple questions I would like to have resolved prior to pushing these changes:
- Is it necessary to explicitly call COMMIT on the transaction at the bottom of the script?
- Is it necessary to explicitly check for errors and call ROLLBACK at the bottom, or will simply using a transaction provide this effect?
Yes.
Yes.
You should also investigate SET XACT_ABORT ON
. SET XACT_ABORT ON
instructs SQL Server to rollback the entire transaction and abort the batch when a run-time error occurs.
This article Error Handling in SQL 2005 and Later is worth reading.
精彩评论