@@trancount Should I Use?

Hello Experts
Am Using SQL Server 2008 and I use the following approach/syntax  in my code to handle a transaction

CREATE  PROC TestTrns (@A..,@B.....)
AS
Begin Try
 Begin Tran
   Insert into T1 ...........................
   Update T2 Set .........................
   Update T3 Set ........................
 COMMIT TRAN
 End TRY
Begin CATCH
    ROLLBACK TRAN
    DECLARE @ErrorMessage_ NVARCHAR(4000);
    DECLARE @ErrorSeverity_ INT;
    DECLARE @ErrorState_ INT;
    SELECT@ErrorMessage_=ERROR_MESSAGE(),@ErrorSeverity_=ERROR_SEVERITY(),@ErrorState_ = ERROR_STATE();
   
     RAISERROR (@ErrorMessage_,@ErrorSeverity_,@ErrorState_  )

END CATCH

My Question Is
- Is the above code is enough to handlijng error in a SP and transaction ? If yes Why some Developer use a variable like @@Trancount when handling Error
If you recommend to use @@Trancount please use my code and place it into the right place because really i couldnt get the usage of It
Thanks
-  
ali_alannahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cenjoy100Commented:
The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT. COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1.
This is the basic use of @@TRANCOUNT and good to use if you have multiple transcations
0
AlokJain0412Commented:
CREATE  PROC TestTrns (@A..,@B.....)
AS
Begin Tran
Begin Try
   Insert into T1 ...........................
   Update T2 Set .........................
   Update T3 Set ........................
End TRY
Begin CATCH
    DECLARE @ErrorMessage_ NVARCHAR(4000);
    DECLARE @ErrorSeverity_ INT;
    DECLARE @ErrorState_ INT;
    SELECT@ErrorMessage_=ERROR_MESSAGE(),@ErrorSeverity_=ERROR_SEVERITY(),@ErrorState_ = ERROR_STATE();
   
     RAISERROR (@ErrorMessage_,@ErrorSeverity_,@ErrorState_  )
IF @@TRANCOUNT > 0
Begin
        ROLLBACK TRANSACTION
End
END CATCH

IF @@TRANCOUNT > 0
Begin
    COMMIT TRANSACTION
End

You Please try and let me know
For better understanding Please go through following thread
http://www.sqlservercentral.com/Forums/Topic905616-392-1.aspx#bm906213

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ali_alannahAuthor Commented:
Not clear
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.