February 5, 2014 by Kenneth Fisher
Transactions are great tools that every DBA and developer should learn how to use. Unfortunately not everything can be put inside a transaction. There are a handful of commands that won’t work inside a transaction. CREATE, ALTER and DROP DATABASE for example. The full list of commands can be found here.
When you try to run one of these commands inside a transaction you will get the following error.
BEGIN TRANSACTION CREATE DATABASE NoTransaction COMMIT
Msg 3902, Level 16, State 1, Line 1
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
I suspect that anything that affects the file system is not going to work in a transaction. Xp_cmdshell for example is not on the list and doesn’t given an error but the results aren’t rolled back.
BEGIN TRANSACTION EXEC xp_cmdshell 'dir c:\ > c:\temp\dir.txt' ROLLBACK
EXEC xp_cmdshell 'dir c:\temp\dir.txt'
You will note that dir.txt exists even though the transaction was rolled back. It’s fairly obvious, but still something that should be kept in mind. According to the link above UPDATE STATISTICS is another one of those things that will not throw an error but still doesn’t get rolled back.
Transactions are a big subject which I’m going to explore over several posts. I am by no means going to cover the subject exhaustively but if you have any subjects you would like me to cover or think I’ve missed something feel free to comment or email me.
- Transactions: Rolling back a nested transaction.
- Transactions: Rolling back part of a transaction.
- Transactions: Rolling back a transaction inside a stored procedure.
- Transactions: What are they?
- Transactions: Who, What and Where
- Transactions: Creating a single restore point across multiple databases.