Rollback in SQL Server

Keywords: SQL Server SQL

USE [TestDB]
GO
/****** Object: Table [dbo].[Person] script date: 11 / 23 / 2008 13:37:48******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Person](
    [PersonId] [nchar](18) NOT NULL,
    [PersonName] [nchar](20) NOT NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [PersonId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

By default, if an error occurs in the execution of a transaction, only the error operation statement is rolled back (that is, if this statement is not executed, it will not be counted as a rollback). The correct operation statement before or after the error will still be committed. Such as:

Use TestDB

Begin TransAction
    Insert Into Person(PersonId,PersonName)
                Values('1','Name1')
    Insert Into Person(PersonId,PersonName)
                Values('1','Name1')
    Insert Into Person(PersonId,PersonName)
                Values('3','Name3')
Commit TransAction
/*
    Select There are '1','Name1' and '3','Name3' next,
    It means that only the mistake in the second sentence has been cancelled
*/

Method 1 of rollback all: open Xact? Abort

Use TestDB
SET XACT_ABORT ON -- open
Begin TransAction
    Insert Into Person(PersonId,PersonName)
                Values('1','Name1')
    Insert Into Person(PersonId,PersonName)
                Values('1','Name1')
    Insert Into Person(PersonId,PersonName)
                Values('3','Name3')
Commit TransAction
/*
    When set Xact "abort is ON,
    If the execution of a transact SQL statement results in a runtime error,
    The entire transaction is terminated and rolled back. 
    It is OFF by default.
*/

Rollback all method 2: use Try...Catch

Use TestDB
Begin Try
    Begin TransAction
        Insert Into Person(PersonId,PersonName)
                    Values('1','Name1')
        Insert Into Person(PersonId,PersonName)
                    Values('1','Name1')
        Insert Into Person(PersonId,PersonName)
                    Values('3','Name3')
    Commit TransAction
End Try
Begin Catch
    Rollback TransAction
End Catch
/*
    Use TryCatch to catch exceptions.
    If an error generated within a TRY block invalidates the state of the current transaction,
    The transaction is classified as uncommitable.
    If the error that normally aborts a transaction outside of a TRY block occurs within a TRY,
    This causes the transaction to go uncommitted.
    Uncommitted transactions can only be read or ROLLBACK TRANSACTION.
    The transaction cannot perform any transact SQL statements that may generate write operations or commit transactions.
    If the transaction is classified as uncommitted, the XACT_STATE function returns a value of - 1.
*/

Rollback all method 3: custom error variable

Use TestDB
Declare @tranError int -- Defining variables
Set @tranError=0
    Begin TransAction
        Insert Into Person(PersonId,PersonName)
                    Values('1','Name1')
            Set @tranError = @tranError + @@Error
        Insert Into Person(PersonId,PersonName)
                    Values('1','Name1')
            Set @tranError = @tranError + @@Error
        Insert Into Person(PersonId,PersonName)
                    Values('3','Name3')
            Set @tranError = @tranError + @@Error
    If @tranError = 0
        Commit TransAction
    Else
        Rollback TransAction
/*
    Customize a variable to determine whether an error occurred at last.
*/

Finally, it should be noted that if a TransAction writes "Begin TransAction" but does not write "Commit TransAction" or "Rollback TransAction", the data of relevant operations (maybe tables, maybe columns, which I haven't tested yet...) Will be locked... The solution for locking is to execute Commit or Rollback TransAction separately

Posted by Lodar on Sun, 05 Jan 2020 05:05:58 -0800