Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 674531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:43:03+00:00 2026-05-14T00:43:03+00:00

I was creating a stored procedure and I see some differences between my methodology

  • 0

I was creating a stored procedure and I see some differences between my methodology and my colleague’s.

I am using SQL Server 2005

  1. My Stored procedure looks like this

    BEGIN TRAN
        BEGIN TRY
            INSERT INTO Tags.tblTopic 
                (Topic, TopicCode, Description)
                VALUES(@Topic, @TopicCode, @Description)
    
    
            INSERT INTO Tags.tblSubjectTopic
                (SubjectId, TopicId)
                VALUES(@SubjectId, @@IDENTITY)
          COMMIT TRAN
    END TRY
    
    BEGIN CATCH
            DECLARE @Error VARCHAR(1000)
            SET @Error= 'ERROR NO : '+ERROR_NUMBER() + ', LINE NO : '+ ERROR_LINE() + ', ERROR MESSAGE : '+ERROR_MESSAGE()
            PRINT @Error
            ROLLBACK TRAN
    END CATCH
    
  2. And my colleague’s way of writing looks like the below one

    BEGIN TRY
        BEGIN TRAN
            INSERT INTO Tags.tblTopic 
                (Topic, TopicCode, Description)
                VALUES(@Topic, @TopicCode, @Description)
    
    
            INSERT INTO Tags.tblSubjectTopic
                (SubjectId, TopicId)
                VALUES(@SubjectId, @@IDENTITY)
       COMMIT TRAN
    END TRY
    
    BEGIN CATCH
            DECLARE @Error VARCHAR(1000)
            SET @Error= 'ERROR NO : '+ERROR_NUMBER() + ', LINE NO : '+ ERROR_LINE() + ', ERROR MESSAGE : '+ERROR_MESSAGE()
            PRINT @Error
            ROLLBACK TRAN
    END CATCH
    

Here the only difference between the two methods is the position of Begin TRAN.
According to me my colleague’s method should not work when an exception occurs i.e. Rollback should not get executed because TRAN doesn’t have scope in method 2. But when I tried to run both the methods, they were working in the same way.

In Method 1, scope of TRAN is outside of try block so it should be visible in both try block and catch block and should give result as per the scope methodology of programming works.

In Method 2, scope of TRAN is limited within Try block so Commit and Rollback should occur within the try block and should throw exception when a Rollback with no Begin Tran exists in catch block, but this is also working perfectly.

I am confused about how TRANSACTION works. Is it scope-free?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:43:04+00:00Added an answer on May 14, 2026 at 12:43 am

    Transactions are not “scoped” in the way that programming languages are.

    Transactions are nested for the current connection. Each BEGIN TRAN starts a new transaction and this transaction ends whenever a COMMIT or ROLLBACK is called, it does not matter where in your stored proc this is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a stored procedure, when is #Temptable created in SQL Server 2005? When creating
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
I am creating a simple stored procedure in VS 2010/SQL Server 2008 as follows:
I'm creating a stored procedure to return search results where some of the parameters
I'm a newbie when it comes to SQL. When creating a stored procedure with
I'm having trouble creating this stored procedure on IBM Informix Dynamic Server Version 10.00.FC9
I am creating a SQL 2008 R2 stored procedure to duplicate a row and
I am creating stored procedures for inserting and updating data in my SQL Server
If you're creating a temporary table within a stored procedure and want to add
I am creating a stored procedure that produces reports based on data in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.