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.
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.