You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Transact-SQL local variable is an object that can hold a single data value of a specific type. Variables in batches and scripts are typically used:
24
24
@@ -33,8 +33,9 @@ The following script creates a small test table and populates it with 26 rows. T
33
33
34
34
* Control how many rows are inserted by controlling how many times the loop is executed.
35
35
* Supply the value inserted into the integer column.
36
-
* Function as part of the expression that generates letters to be inserted into the character column.
37
-
```
36
+
* Function as part of the expression that generates letters to be inserted into the character column.
37
+
38
+
```sql
38
39
-- Create the table.
39
40
CREATETABLETestTable (cola int, colb char(3));
40
41
GO
@@ -81,19 +82,19 @@ The DECLARE statement initializes a Transact-SQL variable by:
81
82
* Assigning a system-supplied or user-defined data type and a length. For numeric variables, a precision and scale are also assigned. For variables of type XML, an optional schema collection may be assigned.
82
83
* Setting the value to NULL.
83
84
84
-
For example, the following **DECLARE** statement creates a local variable named **@mycounter** with an int data type.
85
-
```
85
+
For example, the following **DECLARE** statement creates a local variable named **@mycounter** with an int data type.
86
+
```sql
86
87
DECLARE @MyCounter int;
87
88
```
88
89
To declare more than one local variable, use a comma after the first local variable defined, and then specify the next local variable name and data type.
89
90
90
-
For example, the following **DECLARE** statement creates three local variables named **@LastName**, **@FirstName** and **@StateProvince**, and initializes each to NULL:
91
-
```
91
+
For example, the following **DECLARE** statement creates three local variables named **@LastName**, **@FirstName** and **@StateProvince**, and initializes each to NULL:
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. For example, the following script generates a syntax error because the variable is declared in one batch and referenced in another:
96
-
```
96
+
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. For example, the following script generates a syntax error because the variable is declared in one batch and referenced in another:
97
+
```sql
97
98
USE AdventureWorks2014;
98
99
GO
99
100
DECLARE @MyVariable int;
@@ -109,9 +110,9 @@ FROM HumanResources.Employee
109
110
WHERE BusinessEntityID = @MyVariable;
110
111
```
111
112
112
-
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.
113
+
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.
113
114
114
-
```
115
+
```sql
115
116
DECLARE @MyVariable int;
116
117
SET @MyVariable =1;
117
118
EXECUTE sp_executesql N'SELECT @MyVariable'; -- this produces an error
@@ -121,9 +122,9 @@ EXECUTE sp_executesql N'SELECT @MyVariable'; -- this produces an error
121
122
122
123
When a variable is first declared, its value is set to NULL. To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.
123
124
124
-
To assign a variable a value by using the SET statement, include the variable name and the value to assign to the variable. This is the preferred method of assigning a value to a variable. The following batch, for example, declares two variables, assigns values to them, and then uses them in the `WHERE` clause of a `SELECT` statement:
125
+
To assign a variable a value by using the SET statement, include the variable name and the value to assign to the variable. This is the preferred method of assigning a value to a variable. The following batch, for example, declares two variables, assigns values to them, and then uses them in the `WHERE` clause of a `SELECT` statement:
125
126
126
-
```
127
+
```sql
127
128
USE AdventureWorks2014;
128
129
GO
129
130
-- Declare two variables.
@@ -142,9 +143,9 @@ WHERE FirstName = @FirstNameVariable
142
143
GO
143
144
```
144
145
145
-
A variable can also have a value assigned by being referenced in a select list. If a variable is referenced in a select list, it should be assigned a scalar value or the SELECT statement should only return one row. For example:
146
+
A variable can also have a value assigned by being referenced in a select list. If a variable is referenced in a select list, it should be assigned a scalar value or the SELECT statement should only return one row. For example:
146
147
147
-
```
148
+
```sql
148
149
USE AdventureWorks2014;
149
150
GO
150
151
DECLARE @EmpIDVariable int;
@@ -157,9 +158,9 @@ GO
157
158
> [!WARNING]
158
159
> If there are multiple assignment clauses in a single SELECT statement, SQL Server does not guarantee the order of evaluation of the expressions. Note that effects are only visible if there are references among the assignments.
159
160
160
-
If a SELECT statement returns more than one row and the variable references a non-scalar expression, the variable is set to the value returned for the expression in the last row of the result set. For example, in the following batch **@EmpIDVariable** is set to the **BusinessEntityID** value of the last row returned, which is 1:
161
+
If a SELECT statement returns more than one row and the variable references a non-scalar expression, the variable is set to the value returned for the expression in the last row of the result set. For example, in the following batch **@EmpIDVariable** is set to the **BusinessEntityID** value of the last row returned, which is 1:
0 commit comments