{"id":118,"date":"2012-01-12T10:24:41","date_gmt":"2012-01-12T10:24:41","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/01\/12\/the-rollback-transaction-request-has-no-corresponding-begin-transaction\/"},"modified":"2012-01-12T10:24:41","modified_gmt":"2012-01-12T10:24:41","slug":"the-rollback-transaction-request-has-no-corresponding-begin-transaction","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/01\/12\/the-rollback-transaction-request-has-no-corresponding-begin-transaction\/","title":{"rendered":"The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION."},"content":{"rendered":"

While using transactions in SQL Server sometimes you get the following errors:<\/p>\n

The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
\nThe COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.<\/strong><\/p>\n

Cause For this error:<\/strong>
\nThere is no transaction in progress but the COMMIT\/ROLLBACK TRANSACTION request is issued.<\/p>\n

Replication of the scenario:<\/strong><\/em>
\n[sql]
\nBEGIN TRAN
\nCOMMIT TRAN
\nCOMMIT TRAN
\n[\/sql]<\/p>\n

In the above query the first COMMIT TRAN statement has commited the transaction started by BEGIN TRAN, When the second COMMIT TRAN statement is executed the error The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION<\/strong> occurs.<\/p>\n

Same is the case for ROLLBACK TRAN in the below query.
\n[sql]
\nBEGIN TRAN
\nROLLBACK TRAN
\nROLLBACK TRAN
\n[\/sql]<\/p>\n

Fix For this error:<\/strong><\/em><\/p>\n

As a best practice always check for open transactions before issuing COMMIT TRAN or ROLLBACK TRAN statement.
\n@@TRANCOUNT can be used to find the number of open transactions.<\/p>\n

[sql]
\nIF(@@TRANCOUNT>0)
\nCOMMIT TRAN
\n[\/sql]<\/p>\n

[sql]
\nIF(@@TRANCOUNT>0)
\nROLLBACK TRAN
\n[\/sql]<\/p>\n

This way the commit\/rollback tansaction error can be avoided.<\/strong><\/em><\/p>\n

Other Such Scenarios:<\/strong><\/p>\n

[sql]
\nBEGIN TRAN
\nCOMMIT
\nCOMMIT
\n[\/sql]<\/p>\n

[sql]
\nBEGIN TRAN
\nCOMMIT WORK
\nCOMMIT WORK
\n[\/sql]<\/p>\n

[sql]
\nBEGIN TRAN
\nROLLBACK WORK
\nROLLBACK WORK
\n[\/sql]<\/p>\n","protected":false},"excerpt":{"rendered":"

While using transactions in SQL Server sometimes you get the following errors: The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. Cause For this error: There is no transaction in progress but the COMMIT\/ROLLBACK TRANSACTION request is issued. Replication of the scenario: [sql] BEGIN TRAN COMMIT…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128,3],"tags":[744,745,746,747,748,749,750,751,752,753,754,755,756,757],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/118"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}