Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Transaction syntax

I'm implementing a mission critical stored procedure that will perform UPDATE, DELETE and INSERT and I want to make sure my TRANSACTION is correctly formed.

I've seen some TRANSACTION statements where there's a check after each step. I've also seen this kind where the entire set of steps are simply placed within a singleTRANSACTION block without any "check points" along the way.

Is this a well formed TRANSACTION that will roll back everything i.e. UPDATE, DELETE and INSERT, if there's any error at any point.

Here's the TRANSACTION:

BEGIN TRANSACTION

BEGIN TRY

   UPDATE SomeTable
   SET SomeColumnValue = 123
   WHERE Id = 123456

   DELETE FROM SomeOtherTable
   WHERE Id = 789

   INSERT INTO ThirdTable
      (Column1, Column2)
   VALUE
      ('Hello World', 1234567)  

END TRY
BEGIN CATCH

   ROLLBACK TRANSACTION

END CATCH
like image 825
Sam Avatar asked Feb 13 '18 04:02

Sam


People also ask

Why we use begin Tran in SQL?

BEGIN TRANSACTION represents a point at which the data referenced by a connection is logically and physically consistent. If errors are encountered, all data modifications made after the BEGIN TRANSACTION can be rolled back to return the data to this known state of consistency.

How do I COMMIT a transaction in SQL Server?

Marks the end of a successful implicit or explicit transaction. If @@TRANCOUNT is 1, COMMIT TRANSACTION makes all data modifications since the start of the transaction a permanent part of the database, frees the transaction's resources, and decrements @@TRANCOUNT to 0.

Can SQL function have transactions?

A transaction is a sequence of operations performed (using one or more SQL statements) on a database as a single logical unit of work. The effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database).


1 Answers

You can use a syntax like below. Note that ths syntax also takes care of nested transaction when a another SP with similar structure is called from inside the begin try block

BEGIN TRAN

BEGIN TRY

    UPDATE SomeTable
    SET SomeColumnValue = 123
    WHERE Id = 123456

    DELETE FROM SomeOtherTable
    WHERE Id = 789

    INSERT INTO ThirdTable
    (Column1, Column2)
    VALUE
    ('Hello World', 1234567)  

    COMMIT TRAN

END TRY
BEGIN CATCH

    IF @@TRANCOUNT > 0
    ROLLBACK TRAN;

    INSERT INTO LogError (
                        --ErrorID
                        objectName
                        ,ErrorCode
                        ,ErrorDescription
                        ,ErrorGenerationTime            
                             )
    SELECT  
                        -- autogenerated
                        OBJECT_NAME(@@PROCID)
                        ,ERROR_NUMBER() AS ErrorCode
                        ,'Error of Severity: ' + CAST (ERROR_SEVERITY() AS VARCHAR (4))
                         +' and State: ' + CAST (ERROR_STATE() AS VARCHAR (8))
                         +' occured in Line: ' + CAST (ERROR_LINE() AS VARCHAR (10))
                         +' with following Message: ' + ERROR_MESSAGE() AS ErrorColumnDescription
                        ,GETDATE()
END CATCH
like image 114
DhruvJoshi Avatar answered Oct 03 '22 03:10

DhruvJoshi