In general, I just want to be able to do this:
begin transaction
begin transaction
select 'x'
rollback
rollback
The reason is, I have a stored proc with code like this:
begin transaction
--Do stuff
If(problem)
begin
rollback
end else begin
commit
end
It works just like I want it to, but I want to test it by doing this:
begin transaction
exec MyStoredProc
rollback
When the stored proc executes a rollback it seems to close both transactions, and then my rollback outside the stored proc fails.
There is no such thing as an autonomous transaction in SQL Server. Nesting transactions serves to increase
@@TRANCOUNT, but that’s about it. As @marc_s suggested in his comment,ROLLBACKdoes not apply solely to the current transaction scope/level, it applies to the whole thing.This request was rejected in 2008:
http://connect.microsoft.com/SQLServer/feedback/details/324569/add-support-for-true-nested-transactions
However, this request is still active – so maybe there is hope:
http://connect.microsoft.com/SQLServer/feedback/details/296870/add-support-for-autonomous-transactions
As for your outer rollback failing, you can always check for this first instead of doing it blindly, e.g.: