BEGIN...END (Transact-SQL)BEGIN...END (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

含括一系列的 Transact-SQLTransact-SQL 陳述式,以便執行一組 Transact-SQLTransact-SQL 陳述式。Encloses a series of Transact-SQLTransact-SQL statements so that a group of Transact-SQLTransact-SQL statements can be executed. BEGIN 和 END 是流程控制語言關鍵字。BEGIN and END are control-of-flow language keywords.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

BEGIN  
    { sql_statement | statement_block }   
END  

引數Arguments

{ sql_statement | statement_block }{ sql_statement | statement_block }
這是藉由使用陳述式區塊所定義的任何有效 Transact-SQLTransact-SQL 陳述式或陳述式分組。Is any valid Transact-SQLTransact-SQL statement or statement grouping as defined by using a statement block.

RemarksRemarks

BEGIN...END 區塊可以有巢狀結構。BEGIN...END blocks can be nested.

雖然 BEGIN...END 區塊中所有的 Transact-SQLTransact-SQL 陳述式都是有效的陳述式,但某些 Transact-SQLTransact-SQL 陳述式不應在同一批次或陳述式區塊中分成一組。Although all Transact-SQLTransact-SQL statements are valid within a BEGIN...END block, certain Transact-SQLTransact-SQL statements should not be grouped together within the same batch, or statement block.

範例Examples

在下列範例中,BEGINEND 會定義一系列同時執行的 Transact-SQLTransact-SQL 陳述式。In the following example, BEGIN and END define a series of Transact-SQLTransact-SQL statements that execute together. 如果沒有 BEGIN...END 區塊,這兩個 ROLLBACK TRANSACTION 陳述式都要執行,而且都會傳回這兩個 PRINT 訊息。If the BEGIN...END block were not included, both ROLLBACK TRANSACTION statements would execute and both PRINT messages would be returned.

USE AdventureWorks2012;  
GO  
BEGIN TRANSACTION;  
GO  
IF @@TRANCOUNT = 0  
BEGIN  
    SELECT FirstName, MiddleName   
    FROM Person.Person WHERE LastName = 'Adams';  
    ROLLBACK TRANSACTION;  
    PRINT N'Rolling back the transaction two times would cause an error.';  
END;  
ROLLBACK TRANSACTION;  
PRINT N'Rolled back the transaction.';  
GO  
/*  
Rolled back the transaction.  
*/  

範例:Azure SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse and 平行處理資料倉儲Parallel Data Warehouse

在下列範例中,BEGINEND 會定義一系列同時執行的 SQLSQL 陳述式。In the following example, BEGIN and END define a series of SQLSQL statements that run together. 如果未包含 BEGIN...END 區塊,則下列範例將處於持續不斷的迴圈狀態。If the BEGIN...END block are not included, the following example will be in a continuous loop.

-- Uses AdventureWorks  
  
DECLARE @Iteration Integer = 0  
WHILE @Iteration <10  
BEGIN  
    SELECT FirstName, MiddleName   
    FROM dbo.DimCustomer WHERE LastName = 'Adams';  
SET @Iteration += 1  
END;  
  

另請參閱See Also

ALTER TRIGGER (Transact-SQL) ALTER TRIGGER (Transact-SQL)
流程控制語言 (Transact-SQL) Control-of-Flow Language (Transact-SQL)
CREATE TRIGGER (Transact-SQL) CREATE TRIGGER (Transact-SQL)
END (BEGIN...END) (Transact-SQL)END (BEGIN...END) (Transact-SQL)