Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transaction rollback working yet an error is still getting generated

I’m trying to run this T-SQL in SQL Server Management Studio:

BEGIN TRAN;

DELETE FROM [TestDB].[dbo].[TestTable];

IF @@ROWCOUNT > 1
BEGIN
    ROLLBACK COMMIT;
END
ELSE
BEGIN
    COMMIT TRAN;
END

The TestTable has more than 1 row so the ROLLBACK COMMIT; line is getting executed. I also note that the table doesn’t get cleared of its records which indicates that the rollback worked. However, I am still getting the below error on the line where the rollback occurs.

(13 rows affected)
Msg 3902, Level 16, State 1, Line 5
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What could be the cause for the error even though everything seems to be functioning as expected?

>Solution :

This problem line contains 2 separate statements:

ROLLBACK COMMIT;

It is functioonally identical to

ROLLBACK;
COMMIT;

You get the error because the transaction was rolled back by the first statement so there is no open transaction to commit by the second statement.

Remove the COMMIT to rollback the transaction and avoid the error.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading