Sometimes I saw the following code snippet. When is the if @@trancount > 0
necessary with begin try
? Both of them? Or it's a safe way(best practice) to check it always in case it's rollback before the check?
begin tran
begin try
... just several lines of sql ...
if @@trancount > 0 commit tran
end try
begin catch
if @@trancount > 0 rollback tran
end catch
I can think of a few scenarios to consider when dealing with @@trancount
:
I believe Remus Rusanu's Exception handling and nested transactions handles all these possibilities.