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
Copy file name to clipboardExpand all lines: docs/t-sql/language-elements/variables-transact-sql.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,12 +37,12 @@ The following script creates a small test table and populates it with 26 rows. T
37
37
38
38
```sql
39
39
-- Create the table.
40
-
CREATETABLETestTable (cola int, colb char(3));
40
+
CREATETABLETestTable (cola INT, colb CHAR(3));
41
41
GO
42
42
SET NOCOUNT ON;
43
43
GO
44
44
-- Declare the variable to be used.
45
-
DECLARE @MyCounter int;
45
+
DECLARE @MyCounter INT;
46
46
47
47
-- Initialize the variable.
48
48
SET @MyCounter =0;
@@ -84,20 +84,20 @@ The DECLARE statement initializes a Transact-SQL variable by:
84
84
85
85
For example, the following **DECLARE** statement creates a local variable named **\@mycounter** with an int data type.
86
86
```sql
87
-
DECLARE @MyCounter int;
87
+
DECLARE @MyCounter INT;
88
88
```
89
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.
90
90
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:
97
97
```sql
98
98
USE AdventureWorks2014;
99
99
GO
100
-
DECLARE @MyVariable int;
100
+
DECLARE @MyVariable INT;
101
101
SET @MyVariable =1;
102
102
-- Terminate the batch by using the GO keyword.
103
103
GO
@@ -113,7 +113,7 @@ WHERE BusinessEntityID = @MyVariable;
113
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.
114
114
115
115
```sql
116
-
DECLARE @MyVariable int;
116
+
DECLARE @MyVariable INT;
117
117
SET @MyVariable =1;
118
118
EXECUTE sp_executesql N'SELECT @MyVariable'; -- this produces an error
119
119
```
@@ -128,8 +128,8 @@ To assign a variable a value by using the SET statement, include the variable na
128
128
USE AdventureWorks2014;
129
129
GO
130
130
-- Declare two variables.
131
-
DECLARE @FirstNameVariable nvarchar(50),
132
-
@PostalCodeVariable nvarchar(15);
131
+
DECLARE @FirstNameVariable NVARCHAR(50),
132
+
@PostalCodeVariable NVARCHAR(15);
133
133
134
134
-- Set their values.
135
135
SET @FirstNameVariable = N'Amy';
@@ -148,7 +148,7 @@ A variable can also have a value assigned by being referenced in a select list.
148
148
```sql
149
149
USE AdventureWorks2014;
150
150
GO
151
-
DECLARE @EmpIDVariable int;
151
+
DECLARE @EmpIDVariable INT;
152
152
153
153
SELECT @EmpIDVariable =MAX(EmployeeID)
154
154
FROMHumanResources.Employee;
@@ -163,7 +163,7 @@ If a SELECT statement returns more than one row and the variable references a no
0 commit comments