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
Encloses a series of [!INCLUDE [tsql](../../includes/tsql-md.md)] statements so that a group of [!INCLUDE [tsql](../../includes/tsql-md.md)] statements can be executed in a logical block of code. `BEGIN` and `END` are control-of-flow language keywords.
31
+
Encloses a sequence of [!INCLUDE [tsql](../../includes/tsql-md.md)] statements into a logical block of code. Note this use of "`BEGIN`" is unrelated to the `BEGIN TRANSACTION` and `BEGIN ATOMIC` statements.
`BEGIN...END` blocks are often used with a preceding flow-control statement such as `IF`, `ELSE` and `WHILE`, but these blocks can also be used without any preceding flow-control to aesthetically group sequences of statements in a way similar to an anonymous scope `{ ... }` in C-style languages except that `BEGIN...END` blocks do not create a new lexical scope.
* The use of semicolons after the `BEGIN` and `END` keywords is optional [but recommended]((../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)), excepting for some cases where they are required, such as when a CTE (`WITH`) or `THROW` statement is used within a block.
46
+
* Using a semicolon after `BEGIN` can help avoid potential confusion with the `BEGIN TRANSACTION` or `BEGIN ATOMIC` statements.
47
+
Using a semicolon after `END` ensures that any subsequent statement, in particular the `WITH` keyword or `THROW` statement, will not need any preceding semicolon.
48
+
*`BEGIN...END` must contain at least one statement: attempting to use an empty `BEGIN...END` block will result in a syntax error, even when each keyword is used with a semicolon terminator.
49
+
43
50
## Arguments
44
51
45
52
#### { *sql_statement* | *statement_block* }
@@ -48,51 +55,77 @@ Any valid [!INCLUDE [tsql](../../includes/tsql-md.md)] statement or statement gr
48
55
49
56
## Remarks
50
57
51
-
`BEGIN...END` blocks can be nested.
58
+
*`BEGIN...END` blocks can be nested.
59
+
*`BEGIN...END` blocks cannot span multiple batches, i.e. the `GO` batch separator cannot be used inside a `BEGIN...END` block.
60
+
*`BEGIN...END` blocks do not define any lexical scope: a variable declared within a block will be visible throughout the parent batch and not just within the block containing the `DECLARE` statement.
61
+
* Using a `BEGIN...END` block to group statements does not imply all that all statements in the group will be executed atomically: When a batch runs outside of a transaction and an error is raised or an exception is thrown by the 2nd statement of a multi-statement `BEGIN...END` block then the 1st statement will not be rolled-back.
62
+
* To avoid having a (syntactically invalid) empty `BEGIN...END` block, you may use a `GOTO` label as a "no-op" placeholder statement.
52
63
53
-
Although all [!INCLUDE [tsql](../../includes/tsql-md.md)] statements are valid within a `BEGIN...END` block, certain [!INCLUDE [tsql](../../includes/tsql-md.md)] statements shouldn't be grouped together within the same batch, or statement block.
64
+
Although all [!INCLUDE [tsql](../../includes/tsql-md.md)] statements are valid within a `BEGIN...END` block, certain [!INCLUDE [tsql](../../includes/tsql-md.md)] statements shouldn't be grouped together within the same batch, or statement block<!-- TODO: Is there an authoritative list of statements that should not be used? -->.
54
65
55
66
## Examples
56
67
57
-
In the following example, `BEGIN` and `END` define a series of [!INCLUDE [tsql](../../includes/tsql-md.md)] statements that execute together. If the `BEGIN...END` block isn't included, both `ROLLBACK TRANSACTION` statements would execute, and both `PRINT` messages would be returned.
68
+
In the following example, `BEGIN` and `END` define sequences of logically related [!INCLUDE [tsql](../../includes/tsql-md.md)] statements to be executed in-order; nested blocks are also demonstrated.
58
69
59
70
```sql
60
71
USE AdventureWorks2022;
61
-
GO
62
72
63
-
BEGIN TRANSACTION
64
73
GO
65
74
66
-
IF @@TRANCOUNT =0
67
-
BEGIN
68
-
SELECT FirstName, MiddleName
69
-
FROMPerson.Person
70
-
WHERE LastName ='Adams';
75
+
DECLARE @personId int= ( SELECTp.BusinessEntityIDFROMPerson.PersonAS p WHEREp.rowguid= {guid'92C4279F-1207-48A3-8448-4636514EB7E2'} );
76
+
IF( @personId IS NULL ) THROW 50001, 'Person not found.', 1;
SET @nameConcat = CONCAT( @nameConcat, N' (', @emails, N')' );
99
+
END;
100
+
101
+
END;
71
102
72
-
ROLLBACK TRANSACTION;
103
+
/* BEGIN...END blocks do not define a lexical scope, so even though @nameAndEmails is declared above, it is still in-scope after the END keyword. */
104
+
SELECT @nameConcat AS NameAndEmails;
105
+
```
73
106
74
-
PRINT N'Rolling back the transaction two times would cause an error.';
75
-
END;
107
+
## Empty blocks:
76
108
77
-
ROLLBACK TRANSACTION;
109
+
If you are generating Dynamic SQL with a `BEGIN...END` block such that it's simpler for your program to always render the `BEGIN...END` keywords then you may use a `GOTO` label as a "NOOP" or placeholder statement:
78
110
79
-
PRINT N'Rolled back the transaction.';
80
-
GO
111
+
```sql
112
+
BEGIN;
113
+
unusedNoopLabel:
114
+
END;
81
115
```
82
116
83
117
## Examples: [!INCLUDE [ssazuresynapse-md](../../includes/ssazuresynapse-md.md)] and [!INCLUDE [ssPDW](../../includes/sspdw-md.md)]
84
118
85
-
In the following example, `BEGIN` and `END` define a series of [!INCLUDE [DWsql](../../includes/dwsql-md.md)] statements that run together. If the `BEGIN...END`block isn't included, the following example runs in a continuous loop.
119
+
In the following example, `BEGIN` and `END` define a series of [!INCLUDE [DWsql](../../includes/dwsql-md.md)] statements that run together. If the `BEGIN` and `END`keywords are commented-out then the following example will run forever in an infinite loop because only the `SELECT` query will be looped by the `WHILE` statement while the `SET @Iteration += 1` statement will never be reached.
0 commit comments