Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables scope which are defined within a while block in stored procedures - SQl Server

I've come across a interesting scenario (at least for me) in a stored procedure. Would like to have experts opinion and thoughts on it.

DECLARE @loopcounter INT SET @loopcounter=10  WHILE @loopcounter > 0 BEGIN   DECLARE @insidevalue int    IF (@loopcounter%2 = 0)   SET @insidevalue = @loopcounter    PRINT 'Value_' + CAST(@insidevalue AS NVARCHAR) + '_'    SET @loopcounter = @loopcounter - 1 END 

I was expecting this block will give the output as below

Value_10_  Value_ _  Value_8_  Value_ _  Value_6_  Value_ _  Value_4_  Value_ _  Value_2_  Value_ _ 

Instead I got output as below:

Value_10_

Value_10_

Value_8_

Value_8_

Value_6_

Value_6_

Value_4_

Value_4_

Value_2_

Value_2_

I thought if I declare a variable inside a while block, then for every iteration it will reset the value to NULL or default value (from c# background).

If this is by design then my question is how does SQLServer treat 'DECLARE' statement for that variable inside while block? Does it ignore it as the variable is already in memory?

Can somebody please explain me this behavior?

like image 764
JPReddy Avatar asked May 18 '11 14:05

JPReddy


People also ask

What is the scope of the variable in SQL?

The scope of a variable is the range of Transact-SQL statements that can reference the variable. The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared.

How many types of variable scope are present in SQL?

Variable Scope in PL/SQL: There are two types of variable scope: Local Variable: Local variables are the inner block variables which are not accessible to outer blocks. Global Variable: Global variables are declared in outermost block.

Which keyword is used to declare a variable within a procedure in SQL?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared.

Can we use variables in stored procedure?

A variable is a named data object whose value can change during the stored procedure execution. You typically use variables in stored procedures to hold immediate results. These variables are local to the stored procedure. Before using a variable, you must declare it.


2 Answers

The variable scope is the whole batch in this case a stored procedure.

It isn't re-declared every loop

So this is exactly as expected

Edit:

There is a recent blog article which is quite similar. The author was quickly corrected :-)

like image 100
gbn Avatar answered Sep 22 '22 21:09

gbn


From Transact-SQL Variables

The scope of a variable is the range of Transact-SQL statements that can reference the variable. The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared.

The DECLARE is not in itself an executable statement. The variable declarations are all identified at compile time and the memory reserved for them in the execution context.

If you use the 2008+ Declare and Set syntax. The Set part of the statement will occur every loop iteration however.

DECLARE @loopcounter INT SET @loopcounter=10  WHILE @loopcounter > 0 BEGIN   DECLARE @insidevalue INT = NULL    IF (@loopcounter%2 = 0)   SET @insidevalue = @loopcounter    PRINT 'Value_' + CAST(@insidevalue AS NVARCHAR) + '_'    SET @loopcounter = @loopcounter - 1 END 
like image 26
Martin Smith Avatar answered Sep 22 '22 21:09

Martin Smith