In a recent Usergroup meet that I attended someone asked me this simple yet interesting question about how transactions work and is there any concept like nested transactions inside SQL Server. Though I have talked about this in the past, thought it would be worth revisiting this concept to drive home some learnings again to readers who are new to SQL Server. Let us learn this using the script code as shown below:
-- Create our database for testing
CREATE DATABASE TransactionsDemo;
-- Create a table for testing
CREATE TABLE tbl_SQLAuth (Col1 INT);
SET NOCOUNT ON;
Once the database is created, our next logical step is to create some transactions to test if we can mimic the use of nested transactions.
-- Create an explicit transaction
BEGIN TRAN OuterTransaction;
INSERT INTO tbl_SQLAuth VALUES (1);
-- Create an explicit so called nested transaction
BEGIN TRAN InnerTransaction;
INSERT INTO tbl_SQLAuth VALUES (2);
-- Look at the trancount?
In the example above we started using an explicit transaction called as “OuterTransaction” and then inserted values post which we created yet another transaction called as “InnerTransaction”. The @@TRANCOUNT will show a value of 2 now. Now that we know the transaction count is 2, let us try to rollback the inner transaction alone.
-- Rollback the inner transaction
ROLLBACK TRAN InnerTransaction;
Msg 6401, Level 16, State 1, Line 33
Cannot roll back InnerTransaction. No transaction or savepoint of that name was found.
Now this is interesting to see the error, we have explicitly created a transaction called as “InnerTransaction” in our code and it is getting ignored.
ROLLBACK TRAN OuterTransaction;
Or the below command works:
And it is so strange that this above commands worked. If you try to query the table, it will have no rows.
-- What hapenned?
SELECT COUNT(*) FROM tbl_SQLAuth;
This is an important learning one needs to know when working with ROLLBACK command inside SQL Server. There is nothing like a nested transaction perse inside SQL Server. Go ahead and do the clean up.
-- Cleanup time
DROP DATABASE TransactionsDemo;
I am curious to know from developers out there, have you ever faced this situation in your environment and learnt it the hard way? If you can share your learning, it would be great.
Reference: Pinal Dave (https://blog.sqlauthority.com)