包含一系列 Transact-SQL 語句,這些語句作為一個組同時執(zhí)行。BEGIN...END 語句塊允許嵌套。
BEGIN
{ sql_statement | statement_block
}
END
{sql_statement | statement_block}
是任何有效的 Transact-SQL 語句或以語句塊定義的語句分組。若要定義語句塊(批處理),采用控制流語言關鍵字 BEGIN 和 END。雖然所有的 Transact-SQL 語句在 BEGIN...END 塊內(nèi)都有效,但有些 Transact-SQL 語句不應組合在同一個批處理(語句塊)中。
Boolean
當一本或更多的書滿足這些條件時,這個示例會給出價格低于 $20 的商業(yè)書籍的列表。否則,SQL Server 會給出一條信息,說明沒有書滿足這個條件,并給出價格低于 $20 的所有書的列表。
SET NOCOUNT OFF
GO
USE pubs
GO
SET NOCOUNT ON
GO
DECLARE @msg varchar(255)
IF (SELECT COUNT(price)
FROM titles
WHERE title_id LIKE 'BU%' AND price < 20) > 0
BEGIN
SET @msg = 'There are several books that are a good value at under $20. These books are: '
PRINT @msg
SET NOCOUNT OFF
SELECT title
FROM titles
WHERE price < 20
END
ELSE
BEGIN
SET @msg = 'There are no books under $20. '
PRINT @msg
SELECT title
FROM titles
WHERE title_id
LIKE 'BU%'
AND
PRICE <10
END
下面是結果集:
There are several books that are a good value at under $20. These books are:
title
------------------------------------------------------------------------
The Busy Executive's Database Guide
Cooking with Computers: Surreptitious Balance Sheets
You Can Combat Computer Stress!
Straight Talk About Computers
Silicon Valley Gastronomic Treats
The Gourmet Microwave
Is Anger the Enemy?
Life Without Fear
Prolonged Data Deprivation: Four Case Studies
Emotional Security: A New Algorithm
Fifty Years in Buckingham Palace Kitchens
Sushi, Anyone?
(12 row(s) affected)
相關文章