Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,

I want to know what is the use of this code

SQL
Declare @mErrMsg varchar(Max)
Begin Try
Begin Tran

--update statements

Commit Tran
End Try
Begin Catch
    If (XACT_STATE()) <> 0
    Begin
        Rollback Tran
    End
    Select @mErrMsg = Error_Message()
    Raiserror(@mErrMsg,16,1)
End Catch
Posted

The code seems to be doing some updates (from a stored procedure, and judging by XACT_STATE it's Transact-SQL), and if the updates fail / the user has another transaction going on at the moment, the update statements are rolled back and an error message is returned
 
Share this answer
 
Xact_State() is used to determine whether there are active user transactions are not.
It may hold 0,1,or -1 value.

O- no active transactions.
1-The current request has an active user transaction. The request can perform any actions, including writing data and committing the transaction.
-1 -There is a current active user transaction, but it has some errors


In the above code, they are using try and catch blocks. All updates/inserts are done inside try block. So whenever error occurs and it comes to catch, then we need to do rollback for entire tran. Before doing rollback, here they are just checking whether there is an active transaction. If there is an active transaction, then it ll rollback and raise error

For eg:

I have a table temp
Column GroupId (int)
COlumn ReleaseDate (datetime)

Now i have a rows like this.

CSS
101 1900-01-02 00:00:00.000
101 1900-01-02 00:00:00.000
102 2012-07-31 00:00:00.000
102 2012-08-02 00:00:00.000
102 2012-08-05 00:00:00.000
103 2012-08-01 00:00:00.000
103 2012-08-06 00:00:00.000



Am trying to update release date column with 'test' which is incorrect. It will throw error.
I have two different blocks of code, one is with transaction, another without tranasaction.

1.
SQL
Declare @mErrMsg varchar(Max)
Begin Try
Begin Tran
 
update temp
set Releasedate='test'
where groupId=101

Commit Tran
End Try
Begin Catch
    If (XACT_STATE()) <> 0
    Begin
        Rollback Tran
        print  'rollback'
    End
    ELse
		print 'not inside XACT_STATE condition'
    Select @mErrMsg = Error_Message()
    Raiserror(@mErrMsg,16,1)
End Catch


The output will be
rollback
Msg 50000, Level 16, State 1, Line 21
Conversion failed when converting date and/or time from character string.

Because there is a begin tran, so there is an active tran and value of XACT_STATE<>0

2. In the scenario I didnt use begin tran


SQL
Declare @mErrMsg varchar(Max)
Begin Try

 
update temp
set Releasedate='test'
where groupId=101

Commit Tran
End Try
Begin Catch
    If (XACT_STATE()) <> 0
    Begin
        Rollback Tran
        print  'rollback'
    End
    ELse
		print 'not inside XACT_STATE condition'
    Select @mErrMsg = Error_Message()
    Raiserror(@mErrMsg,16,1)
End Catch


The output will be
not inside XACT_STATE condition
Msg 50000, Level 16, State 1, Line 22
Conversion failed when converting date and/or time from character string.


Because there is no begin tran, so there is no active tran and value of XACT_STATE will be equal to 0
 
Share this answer
 
v2
Comments
kirthiga S 3-Aug-12 6:05am    
Hi Santhosh,

Can you give any example for this scenario
Santhosh Kumar Jayaraman 3-Aug-12 6:13am    
updated the solution with example.Check
Santhosh Kumar Jayaraman 3-Aug-12 6:23am    
Let me know if you are not clear. If you understood, then mark it as solved
kirthiga S 3-Aug-12 7:11am    
Thank u santhosh.. Its clear
Santhosh Kumar Jayaraman 3-Aug-12 7:41am    
Welcome:)
If some exception will occur while updating your data then it'll check to XACT_STATE(Transact-SQL) [^]. If XACT_STATE is not returning 0(There is no active user transaction for the current request), then the transaction will be rolled back else it will not be.
And finally the error will be raised.


--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900