MS SQL Server Concepts and Programming Question:
Download Questions PDF

How To Use "BEGIN ... END" Statement Structures in MS SQL Server?

Answer:

"BEGIN ... END" statement structure is used to group multiple statements into a single statement block, which can be used in other statement structures as a single statement. For example, a statement block can be used in an "IF ... ELSE ..." statement structure as a single statement.

The tutorial exercise below shows you how to use "BEGIN ... END" statement structures to place multiple statements into an "IF ... ELSE" statement structure:

DECLARE @site_name VARCHAR(40);
SET @site_name = 'SQA';
IF @site_name = 'DBA'
BEGIN
PRINT 'Dropping table: dba_links';
DROP TABLE dba_links;
END
ELSE IF @site_name = 'SQA'
BEGIN
PRINT 'Dropping table: sqa_links';
DROP TABLE sqa_links;
END
ELSE
PRINT 'Unknown site name: '+@site_name;
GO
Dropping table: sqa_links


Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures in MS SQL Server?How To Stop a Loop Early with BREAK Statements in MS SQL Server?