Nested Transaction in SQL Server


Transaction: Transaction is the collection of T-SQL statements which executes in single unit. Transaction begins with a specific T-SQL statement and ends when all T-SQL statements execute successfully. If any statements fail then transaction fails. Transaction has only two types of results success or failed. For more detail, what is Transaction inSQL Server?

Nested Transaction:  Microsoft SQL Server allows starting transaction within transaction which is called Nested Transaction. Nested transaction can be COMMIT but can’t be ROLLBACK.

See Following Example:


CREATE TABLE Employee
(
            ID INT PRIMARY KEY,
            Name VARCHAR(50),
            Emp_Address VARCHAR(100)
)


Run above script to create an Employee table where ID column is PRIMARY KEY. Now see following script.

BEGIN
            BEGIN TRY
                        BEGIN TRANSACTION OUT1
                                    INSERT INTO Employee VALUES(1,'DILIP','NOIDA')                  
                                    BEGIN TRANSACTION INNER1
                                    INSERT INTO Employee VALUES(1,'RAJ','DELHI')                     
                        COMMIT TRANSACTION            
            END TRY
            BEGIN CATCH
                  ROLLBACK TRANSACTION INNER1
            END CATCH
END


This script will generate an error “Cannot roll back INNER1. No transaction or savepoint of that name was found.

Employee has “ID” PRIMARY KEY column in both transactions we trying to insert same value 1 so our transaction failed. In CATCH block we trying to ROLLBACK to INNER1 transaction so above we got an error.
If we are trying to rollback to OUT1 transaction it will run without error.
See following script


BEGIN
            BEGIN TRY
                        BEGIN TRANSACTION OUT1
                                    INSERT INTO Employee VALUES(1,'DILIP','NOIDA')                  
                                    BEGIN TRANSACTION INNER1
                                    INSERT INTO Employee VALUES(1,'RAJ','DELHI')                     
                        COMMIT TRANSACTION            
            END TRY
            BEGIN CATCH
            ROLLBACK TRANSACTION OUT1
            END CATCH
END


Note: Here SQL server allow to rollback only outer transaction and rollback of outer transaction will rollback all inner transaction.
Using Save transaction we can perform partial rollback in transaction

Using SAVE TRANSACTION command we can perform partial rollback, see following script


BEGIN TRANSACTION
INSERT INTO Employee VALUES(1,'DILIP','NOIDA')      
--Save transaction Command
SAVE TRANSACTION INNER1
INSERT INTO Employee VALUES(2,'RAJ','DELHI')         
SELECT * FROM Employee
ROLLBACK TRANSACTION INNER1
COMMIT
SELECT * FROM Employee



Here will find only one record in table after final commit.

No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...