Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server variable scope in a stored procedure

Tags:

sql

sql-server

I would like to declare a variable within an if/else statement in a SQL Server stored procedure. I understand that this is fairly impossible because SQL Server doesn't do memory management with respect to declaration of variables within procedures. Is there a way to have a variable scoped in an if/else statement, then redeclare a variable with the same name in another if/else statement? For example:

create procedure Foo
as
begin  
    if exists (x)
    begin
        declare @bob int
        set bob = 1
    end
    else
    begin
        declare @bob int
        set bob = 2
    end
end
like image 947
gh9 Avatar asked May 13 '11 16:05

gh9


People also ask

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.

Are SQL variables scoped?

Variables have local scope and are only visible within the batch or procedure where they are defined. In the following example, the nested scope created for execution of sp_executesql does not have access to the variable declared in the higher scope and returns and error.

How do you assign a value to a variable in SQL Server stored procedure?

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. Literals, expressions, the result of a query, and special register values can be assigned to variables.

What's the life scope of local variable in SQL Server?

The scope of a local variable is the batch in which it's declared. A table variable isn't necessarily memory resident.


4 Answers

From books online:

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.

However. Nothing keeps you from doing this:

create procedure Foo as begin

declare @bob int

if exists (x)
begin
    set @bob = 1
end
else
begin
    set @bob = 2
end

end
like image 66
RThomas Avatar answered Oct 23 '22 10:10

RThomas


No, SQL is pretty funny/weird like that

Declare the variable before the if exists block of code

so

declare @bob int 
set @bob = 2 

if exists(x) 
begin   
    set @bob = 1 
end

Now, take a look at these examples and try to guess what happens

WHILE 1 = 2 --not true of course
BEGIN
  DECLARE @VAR INT;
END
SET @VAR = 1;

SELECT @VAR;

This of course works, but it is not initialized every time

DECLARE @loop INT
SET @loop = 0

WHILE @loop <=6
BEGIN
        DECLARE @VAR INT
        SET @VAR = COALESCE(@VAR,0) + 1
        SET @loop = @loop +1
END

SELECT @VAR
like image 31
SQLMenace Avatar answered Oct 23 '22 12:10

SQLMenace


is there some reason why you can't do :

declare @bob int 
if exists(x) 
begin   set @bob = 1 end 
else 
begin  set @bob = 2 end 
like image 3
HLGEM Avatar answered Oct 23 '22 12:10

HLGEM


You could resort to using dynamic SQL:

if exists (x)
begin
    exec sp_executesql N'
        declare @bob int
        set @bob = 1
    ';
end
else
begin
    exec sp_executesql N'
        declare @bob int
        set @bob = 2
    ';
end
like image 2
Lukas Eder Avatar answered Oct 23 '22 12:10

Lukas Eder