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 8807795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:27:24+00:00 2026-06-14T02:27:24+00:00

In SSMS 2012, after setting Options > Query Execution > ANSI > SET IMPLICIT_TRANSACTIONS

  • 0

In SSMS 2012, after setting Options > Query Execution > ANSI > SET IMPLICIT_TRANSACTIONS, cf this SO post

I have the following code in a query window:

begin transaction 
    select @@TRANCOUNT
    begin
    declare @someNumber int; set @someNumber = 1; 
    print @someNumber;
    end
rollback 

When I select the whole block and press Execute, I see the expected result, i.e. 1.

However, when I select the first 4 lines and execute, then select line 5, i.e. print @someNumber;, I got the following message:

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable “@someNumber”.

What is exactly the scope of the variable?

I’m baffled. Can someone shed any light or point me to the right direction please?

  • 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-06-14T02:27:26+00:00Added an answer on June 14, 2026 at 2:27 am

    The variable is scoped per batch.

    The scope of a local variable is the batch in which it is declared.

    Each press of “Execute” is a batch. So, for the 2nd run, it isn’t declared

    What are doing with 2 runs is this

    --start of batch 1
        begin transaction 
            select @@TRANCOUNT
            begin
            declare @someNumber int; set @someNumber = 1; 
    --end of batch 1
            GO --separate batch after this
    --start of batch 2
            print @someNumber;
            end
        rollback
    --end of batch 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 and SSMS 2012. I have a stored procedure
In SSMS, I create a database using the following script. When the script execution
I am using SSMS 2008 and trying to insert with this query but am
I'm designing a query in SSMS 2005 which looks something like this: SELECT COUNT(DISTINCT
I am using SSMS 2008 and I have the following Scalar function to take
I am using SSMS 2008 and I have the following scalar function to take
In SSMS 2008 I created a new query window and issued the following statement
UPDATE : I have this SSMS tools pack which wraps everything in this -
I open a new window in SSMS and run this: SET ANSI_DEFAULTS ON GO
I have sql server 2012 express and just installed the latest version of ssms

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.