包含一系列 Transact-SQL 語(yǔ)句,這些語(yǔ)句作為一個(gè)組同時(shí)執(zhí)行。BEGIN...END 語(yǔ)句塊允許嵌套。
BEGIN
{ sql_statement | statement_block
}
END
{sql_statement | statement_block}
是任何有效的 Transact-SQL 語(yǔ)句或以語(yǔ)句塊定義的語(yǔ)句分組。若要定義語(yǔ)句塊(批處理),采用控制流語(yǔ)言關(guān)鍵字 BEGIN 和 END。雖然所有的 Transact-SQL 語(yǔ)句在 BEGIN...END 塊內(nèi)都有效,但有些 Transact-SQL 語(yǔ)句不應(yīng)組合在同一個(gè)批處理(語(yǔ)句塊)中。
Boolean
當(dāng)一本或更多的書滿足這些條件時(shí),這個(gè)示例會(huì)給出價(jià)格低于 $20 的商業(yè)書籍的列表。否則,SQL Server 會(huì)給出一條信息,說(shuō)明沒有書滿足這個(gè)條件,并給出價(jià)格低于 $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
下面是結(jié)果集:
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)
相關(guān)文章