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?
The variable is scoped per batch.
Each press of “Execute” is a batch. So, for the 2nd run, it isn’t declared
What are doing with 2 runs is this