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: learn-pr/wwl-data-ai/get-started-transact-sql-programming/includes/3-describe-batches.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
T-SQL batches are collections of one or more T-SQL statements that are submitted to SQL Server by a client as a single unit. SQL Server operates on all the statements in a batch at the same time when parsing, optimizing, and executing the code.
2
2
3
-
If you're a report writer who typically writes queries using SELECT statements and not procedures, it's still important to understand batch boundaries. These boundaries will affect your work with variables and parameters in stored procedures and other routines. For example, a variable must be declared in the same batch in which it's referenced. It's important, therefore, to recognize what is contained in a batch.
3
+
If you're a report writer who typically writes queries using `SELECT` statements and not procedures, it's still important to understand batch boundaries. These boundaries affect your work with variables and parameters in stored procedures and other routines. For example, a variable must be declared in the same batch in which it's referenced. It's important, therefore, to recognize what is contained in a batch.
4
4
5
-
Batches are delimited by the client application. How you mark the end of a batch depends on the settings of your client. For Microsoft clients including SQL Server Management Studio (SSMS) and SQLCMD the keyword is GO.
5
+
Batches are delimited by the client application. How you mark the end of a batch depends on the settings of your client. For Microsoft clients including SQL Server Management Studio (SSMS) and SQLCMD the keyword is `GO`.
6
6
7
-
In this example, there are two distinct batches each terminated with a GO:
7
+
In this example, there are two distinct batches each terminated with a `GO` statement:
8
8
9
9
```sql
10
10
CREATE NEW <view_name>
@@ -17,16 +17,16 @@ GO
17
17
18
18
The batch terminator **GO** isn't a T-SQL keyword, but is one recognized by SSMS to indicate the end of a batch.
19
19
20
-
When working with T-SQL batches, there are two important considerations to keep in mind:
20
+
Keep two important considerations in mind when you work with T-SQL batches:
21
21
22
-
- Batches are boundaries for variable scope, which means a variable defined in one batch may only be referenced by other code in the same batch
23
-
- Some statements, typically data definition statements such as CREATE VIEW, CREATE FUNCTION, and CREATE PROCEDURE may not be combined with others in the same batch.
22
+
- Batches are boundaries for variable scope, which means a variable defined in one batch can only be referenced by other code in the same batch
23
+
- Some statements, typically data definition statements such as `CREATE VIEW`, `CREATE FUNCTION`, and `CREATE PROCEDURE` can't be combined with others in the same batch.
24
24
25
25
## Working with batches
26
26
27
-
A batch is a collection of T-SQL statements submitted to SQL Server for parsing and execution. Understanding how batches are parsed will be useful in identifying error messages and behavior. When a batch is submitted by a client, such as when you press the Execute button in SSMS, the batch is parsed for syntax errors by the SQL Server engine. Any errors found will cause the entire batch to be rejected; there will be no partial execution of statements within the batch.
27
+
A batch is a collection of T-SQL statements submitted to SQL Server for parsing and execution. Understanding how batches are parsed is useful in identifying error messages and behavior. When a batch is submitted by a client, such as when you press the Execute button in SSMS, the batch is parsed for syntax errors by the SQL Server engine. Any errors found cause the entire batch to be rejected; there is no partial execution of statements within the batch.
28
28
29
-
If the batch passes the syntax check, then SQL Server runs other steps, resolving object names, checking permissions, and optimizing the code for execution. Once this process completes and execution begins, statements succeed or fail individually. This is an important contrast to syntax checking. When a runtime error occurs on one line, the next line may be executed, unless you've added error handling to the code.
29
+
If the batch passes the syntax check, then SQL Server runs other steps, resolving object names, checking permissions, and optimizing the code for execution. Once this process completes and execution begins, statements succeed or fail individually. This is an important contrast to syntax checking. When a runtime error occurs on one line, the next line can still execute, unless you add error handling to the code.
30
30
31
31
For example, the following batch contains a syntax error:
32
32
@@ -43,14 +43,14 @@ Msg 102, Level 15, State 1, Line 1
43
43
Incorrect syntax near 'VALUE'.
44
44
```
45
45
46
-
The error occurred in line 1, but the entire batch is rejected, and execution doesn't continue with line 2. Even if each of the INSERT statements were reversed and the syntax error occurred in the second line, the first line wouldn't be executed because the entire batch would be rejected.
46
+
The error occurred in line 1, but the entire batch is rejected, and execution doesn't continue with line 2. Even if each of the `INSERT` statements were reversed and the syntax error occurred in the second line, the frontline wouldn't be executed because the entire batch would be rejected.
47
47
48
-
Using the previous example, this batch doesn't contain an error:
48
+
In contrast, this corrected batch doesn't contain an error:
49
49
50
50
```sql
51
51
INSERT INTOdbo.t1VALUES(1,2,N'abc');
52
52
INSERT INTOdbo.t1VALUES(2,3,N'def');
53
53
GO
54
54
```
55
55
56
-
In the previous samples, we've used INSERT statements rather than SELECT because it's more common for modification statements to be grouped in batches than SELECT statements.
56
+
The previous samples use `INSERT` statements rather than `SELECT` because modification statements are more commonly grouped in batches.
0 commit comments