I have encountered a small bug(feature) in the SQL Server 2008.
I have a stored procedure where I begin explicit transaction with the name @tran
.... declare @tran varchar(30) = 'exp_trans';
BEGIN TRAN @tran IF @p_param is null ROLLBACK TRAN @tran ....
The problem is, when I set IMPLICIT_TRANSACTIONS ON and invoke the stored procedure, the flow hits the rollback section, I will receive the error
"Msg 6401, Level 16, State 1, Line 10 Cannot roll back exp_tran. No transaction or savepoint of that name was found."
Is this some sort of feature, or what is actually happening? Thank you in advance
I believe this is happening because of nested transactions and SQL Server 2008 does not support rolling back to an inner transaction when transactions are nested:
Your transactions are nested because the implicit transactions setting starts one, the begin trans starts the second (nested) transaction. And that makes the rollback to the named transaction fail.