BEGIN...END (TSQL Statement)
Encloses a series of TSQL statements as a block, so that they are syntaxically treated as a single statement.
This construct is often used in combination with IF...ELSE
and WHILE
statements.
Syntax
BEGIN
<statement_1>
...
<statement_n>
END
Arguments
- <statement_1>…<statement_n>
- series of any valid TSQL statements.
Remarks
BEGIN...END
blocks can be nested.
Examples
CREATE TABLE t (a INT NOT NULL PRIMARY KEY, VARCHAR(50));
GO
DECLARE @i INT = 100;
BEGIN TRANSACTION;
WHILE @i < 105
BEGIN
INSERT INTO t VALUES(@i, 'hello_' + CAST(@i AS VARCHAR));
SET @i += 1;
END
COMMIT TRANSACTION;
SELECT * FROM t;
GO
The result is:
a |b |
-----------+---------------+
100|hello_100 |
101|hello_101 |
102|hello_102 |
103|hello_103 |
104|hello_104 |