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
[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] provides commands that are not [!INCLUDE[tsql](../../includes/tsql-md.md)] statements, but are recognized by the **sqlcmd** and **osql** utilities and [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] Code Editor. These commands can be used to facilitate the readability and execution of batches and scripts.
33
33
34
34
GO signals the end of a batch of [!INCLUDE[tsql](../../includes/tsql-md.md)] statements to the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] utilities.
35
35
36
-
||
37
-
|-|
38
-
|**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] ([!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [current version](http://go.microsoft.com/fwlink/p/?LinkId=299658)), [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)].|
36
+
39
37
40
38
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
41
39
42
40
## Syntax
43
41
44
42
```
45
-
46
43
GO [count]
47
44
```
48
45
@@ -57,25 +54,27 @@ GO [count]
57
54
58
55
A [!INCLUDE[tsql](../../includes/tsql-md.md)] statement cannot occupy the same line as a GO command. However, the line can contain comments.
59
56
60
-
Users must follow the rules for batches. For example, any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword. The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command.
57
+
Users must follow the rules for batches. For example, any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword.
61
58
62
59
```
63
-
USE AdventureWorks2012;
60
+
SELECT @@VERSION;
61
+
sp_who
64
62
GO
63
+
```
64
+
65
+
The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a `GO` command.
66
+
67
+
```
65
68
DECLARE @MyMsg VARCHAR(50)
66
69
SELECT @MyMsg = 'Hello, World.'
67
70
GO -- @MyMsg is not valid after this GO ends the batch.
68
71
69
72
-- Yields an error because @MyMsg not declared in this batch.
70
73
PRINT @MyMsg
71
74
GO
72
-
73
-
SELECT @@VERSION;
74
-
-- Yields an error: Must be EXEC sp_who if not first statement in
75
-
-- batch.
76
-
sp_who
77
-
GO
78
-
```
75
+
```
76
+
77
+
79
78
80
79
[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] applications can send multiple [!INCLUDE[tsql](../../includes/tsql-md.md)] statements to an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] for execution as a batch. The statements in the batch are then compiled into a single execution plan. Programmers executing ad hoc statements in the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] utilities, or building scripts of [!INCLUDE[tsql](../../includes/tsql-md.md)] statements to run through the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] utilities, use GO to signal the end of a batch.
0 commit comments