Transactions: What commands aren’t allowed?

Leave a comment

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,755 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: