Blog Archives

Programming with SQL Server: Variables

Understanding the numerous types of variable SQL Server has to offer, and their appropriate usage is one of the cornerstones of developing effective database code. I recently helped a co-worker fix an error message in his code.

-- Error
Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "@l_INT".

The code snippet looked something like this, which led to a discussion about scope of T-SQL variables and an interesting way to fix this problem.

CREATE TABLE #scope_test1
 (col1 INT);

CREATE TABLE #scope_test2
 (col1 INT);
 GO

DECLARE @l_INT INT = 42;

INSERT INTO #scope_test1 (col1)
 SELECT @l_INT;
 GO

INSERT INTO #scope_test2 (col1)
 SELECT @l_INT;
 GO

T-SQL language supports local variables (their names begin with a single @). 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. A T-SQL batch is a group of statements that SQL Server parses as a single unit. A batch can be delimited by the BEGIN..END statements, or a client tool like SQL Server Management Studio (SSMS) can specify the end of a batch with a GO command (you can set any word to be a batch separator in SSMS, but we will leave that discussion for another time). The names of some Transact-SQL system functions begin with two at signs (@@). They are commonly referred to as Global variables. Global variable names begin with a @@ prefix. You do not need to declare them, since the server constantly maintains them. They are system-defined functions not variables and do not have the same behaviors as variables. All the global variables represent information specific to the server or a current user sessions. Some of the commonly used ones are @@ERROR, @@IDENTITY, @@VERSION. Traditionally, DECLARE command is used to declare a local variable and SET or SELECT used to initialize its value. With SQL Server 2012, both of these tasks can be accomplished in a single statement ;

DECLARE @MyCounter INT = 12 ;

The variables we discussed so far are scalar variables, which can hold a single data value of a specific type. A table variable is a special data type that can be used to store a result set for processing at a later time. table is primarily used for temporary storage of a set of rows returned as the result set of a table-valued function. Functions and variables can be declared to be of type table. table variables can be used in functions, stored procedures, and batches. A DECLARE statement (similar to local scalar variables) is used to DECLARE a table variable. While they behave exactly like local variables, with a well defined scope,they can be thought of as being similar to temporary table, but with several limitations.

The SQLCMD utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses ODBC to execute Transact-SQL batches. Scripting variables can be used in SQLCMD scripts. Scripting variables enable one script to be used in multiple scenarios. The setvar command is used to define scripting variables. Variables that are defined by using the setvar command are stored internally. Scripting variables should not be confused with environment variables that are defined at the command prompt by using SET.

The scope of a SQLCMD scripting variable can span several batches, which can be used to implement variables that don’t go out of scope even when a batch ends. The example below is a simplified demo of the same ; 

:setvar l_int "42"
CREATE TABLE #scope_test1
 (col1 INT);
CREATE TABLE #scope_test2
 (col1 INT);
 GO
INSERT INTO #scope_test1 (col1)
 SELECT $(l_int);
 GO
INSERT INTO #scope_test2 (col1)
 SELECT $(l_int);
 GO

-- Results
(1 row(s) affected)
(1 row(s) affected)

A review of the types of variables the T-SQL language has to offer, has helped refresh my understanding of their appropriate usage.

References:

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: