包含一系列 Transact-SQL 语句,这些语句作为一个组同时执行。BEGIN...END 语句块允许嵌套。
BEGIN
{ sql_statement | statement_block
}
END
{sql_statement | statement_block}
是任何有效的 Transact-SQL 语句或以语句块定义的语句分组。若要定义语句块(批处理),采用控制流语言关键字 BEGIN 和 END。虽然所有的 Transact-SQL 语句在 BEGIN...END 块内都有效,但有些 Transact-SQL 语句不应组合在同一个批处理(语句块)中。
Boolean
当一本或更多的书满足这些条件时,这个示例会给出价格低于 $20 的商业书籍的列表。否则,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)
相关文章