Monday, September 3, 2018

Handling The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION error in MS-SQL


Hi All,



When ever are you using Transactions in your T-SQL statements or in functions or stored procedures you should always make sure there is high chances of Transactions are committed or roll-backed due to one or other reasons and you might encounter the following error every time you trying to commit or rollback the transaction which is already processed.



To avoid the code failure and MS-SQL to raise this error we should better to handle the code by checking the condition if any transaction is active in current session.



we have @@TRANCOUNT global variable which gives the count value of active transactions. we can use this global variable value before committing or roll-backing the transactions.



i.e.



IF(@@TRANCOUNT>0)

  COMMIT TRANSACTION



OR



IF(@@TRANCOUNT>0)

  ROLLBACK TRANSACTION







Happy coding

No comments:

Post a Comment