When deploying your database with SQL Change Automation, you may wonder:
The answer to these questions depends on the type of change you are performing. Here is a break-down of transaction handling behaviour in SQL Change Automation:
|Sequence||Script Directory||Transaction Used||Example script filename|
& Additional Scripts
Where Transaction Used = Yes, SQL Change Automation will automatically wrap your migrations in a single BEGIN TRAN / COMMIT TRAN "block".
If at any point one of these migration fails to deploy, the entire transaction will be rolled-back.
If you wish to use a transaction within your Pre/Post-Deployment scripts, you can perform a BEGIN TRAN yourself, however please ensure that you perform a COMMIT or ROLLBACK at the end of your script to avoid having overlapping transactions.
In the case of migration scripts, programmable objects and additional scripts, SQL Change Automation will confirm that TRANCOUNT() = 0 after each script execution.
This section details some of the conventions used in SQL Change Automation deployments by examining the project artifacts; specifically the T-SQL file that is generated when you perform a Build in Visual Studio, eg.
AdventureWorks\bin\Debug\AdventureWorks.sql. This file contains a concatenated list of all the scripts from your SQL Change Automation project.
The first thing you will see when you open the deployment script is the SqlCmd header:
SqlCmd is a SQL Server utility with a unique scripting syntax that provides us with two key advantages over deploying with plain old T-SQL:
:on error exitdirective ensures that, if any statement within the script raises an unhandled exception, execution is immediately halted at the current batch (rather than simply continuing to the next batch after GO)
The next thing you’ll notice is the use of the
SET XACT_ABORT ON predicate. According to MSDN:
When SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back. When SET XACT_ABORT is OFF, in some cases only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing.
To avoid the need to add excessive amounts of error-handling logic to your scripts, we explicitly set this predicate ON as part of the build and prevent you from setting it to OFF within migrations and programmable objects/additional scripts (unless the Custom transaction-handling mode is used; see below Disabling Automatic Transaction Handling).
Pre & Post-Deployment script XACT_ABORT behavior
In spite of there being no automatic transaction handling for the files contained within the Pre-Deployment and Post-Deployment folders, SQL Change Automation also sets XACT_ABORT to the ON setting within these scripts for consistency of deployment behavior. However, as some features of the T-SQL language (such as the sp_fulltext_load_thesaurus_file statement) require this setting to be OFF, it may be necessary to include a SET XACT_ABORT OFF statement in the header of your Pre/Post Deployment scripts. Note that, for these types of scripts, it is not necessary to include the Custom header metadata described further below.
The combination of SqlCmd and XACT_ABORT ensures that, if an exception occurs at any point during deployment, the execution is halted and the connection dropped at the current statement.
Additionally, scripts that have been executed thus far will be rolled-back:
One thing conspicuously missing here is a
ROLLBACK statement at any point. This is because a rollback is implicit in the use of the XACT_ABORT predicate: when an unhandled error occurs, SQL Server immediately performs a rollback and raises an error to the client (in our case,
For the most part, we hope that the structure we’ve added around your migrations means you’ll never need to worry about how transactions are handled. However there are some use cases for which you might want to exercise greater control over the flow of execution.
For example, say you want to write some details of an error to a log table. Typically, your log records would simply be rolled-back along with your other changes when an error occurs.
But with a TRY/CATCH block, you have the ability to capture exceptions that are raised within your batch and issue a
ROLLBACK yourself. For example:
This code will raise an exception at line 13, causing the prior operations in the batch to be rolled-back. However the new row in the
error_log will persist even after the script is halted (via the
RAISERROR), because it occurs in a separate transaction to the statements within the
INSERTstatement) will be caught by the
CATCHblock, however a reference to a non-existent object will not.
SQL Change Automation runs all of your migrations through the T-SQL compiler during project build, so provided your statements are not being executed as dynamic SQL (eg. using
sp_execute), you should not receive syntax errors at deployment time.
However testing your SQL Change Automation project by doing a full deploy to a test server is still the most foolproof way of validating your T-SQL migrations.
If your use case dictates that the migration be executed outside of a user transaction, or in a transaction that is isolated from any others in the deployment, you can control this behavior at the script level by switching to
Custom transaction handling:
BULK INSERTand you want to commit batches of rows at a time
ALTER DATABASEoperation on the current database
Prior to executing your Custom-flagged migration, SQL Change Automation will
COMMIT any open transactions. After executing that migration, a new transaction will be opened for any remaining migrations that are pending deployment.