Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 3cd64d0

Browse files
authored
Variables, fixing formatting.
1 parent 2f6e189 commit 3cd64d0

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

docs/t-sql/language-elements/variables-transact-sql.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Variables (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "03/14/2017"
4+
ms.date: "09/12/2017"
55
ms.prod: "sql-non-specified"
66
ms.reviewer: ""
77
ms.suite: ""
@@ -18,7 +18,7 @@ ms.author: "rickbyh"
1818
manager: "jhubbard"
1919
---
2020
# Variables (Transact-SQL)
21-
[!INCLUDE[tsql-appliesto-ss2012-xxxx-xxxx-xxx_md](../../includes/tsql-appliesto-ss2012-xxxx-xxxx-xxx-md.md)]
21+
[!INCLUDE[tsql-appliesto-tsql-appliesto-ss2008-all-md](../../includes/tsql-appliesto-ss2008-all-md.md)]
2222

2323
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:
2424

@@ -33,8 +33,9 @@ The following script creates a small test table and populates it with 26 rows. T
3333

3434
* Control how many rows are inserted by controlling how many times the loop is executed.
3535
* 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
3839
-- Create the table.
3940
CREATE TABLE TestTable (cola int, colb char(3));
4041
GO
@@ -81,19 +82,19 @@ The DECLARE statement initializes a Transact-SQL variable by:
8182
* 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.
8283
* Setting the value to NULL.
8384

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
8687
DECLARE @MyCounter int;
8788
```
8889
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.
8990

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:
92+
```sql
9293
DECLARE @LastName nvarchar(30), @FirstName nvarchar(20), @StateProvince nchar(2);
9394
```
9495

95-
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
9798
USE AdventureWorks2014;
9899
GO
99100
DECLARE @MyVariable int;
@@ -109,9 +110,9 @@ FROM HumanResources.Employee
109110
WHERE BusinessEntityID = @MyVariable;
110111
```
111112

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.
113114

114-
```
115+
```sql
115116
DECLARE @MyVariable int;
116117
SET @MyVariable = 1;
117118
EXECUTE sp_executesql N'SELECT @MyVariable'; -- this produces an error
@@ -121,9 +122,9 @@ EXECUTE sp_executesql N'SELECT @MyVariable'; -- this produces an error
121122

122123
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.
123124

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:
125126

126-
```
127+
```sql
127128
USE AdventureWorks2014;
128129
GO
129130
-- Declare two variables.
@@ -142,9 +143,9 @@ WHERE FirstName = @FirstNameVariable
142143
GO
143144
```
144145

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:
146147

147-
```
148+
```sql
148149
USE AdventureWorks2014;
149150
GO
150151
DECLARE @EmpIDVariable int;
@@ -157,9 +158,9 @@ GO
157158
> [!WARNING]
158159
> 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.
159160
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:
161162

162-
```
163+
```sql
163164
USE AdventureWorks2014;
164165
GO
165166
DECLARE @EmpIDVariable int;
@@ -179,4 +180,4 @@ GO
179180

180181
[SELECT @local_variable](../../t-sql/language-elements/select-local-variable-transact-sql.md)
181182

182-
183+

0 commit comments

Comments
 (0)