Home >> FAQs/Tutorials >> SQL Server FAQ

SQL Server FAQ - "BEGIN ... END" - Statement Structures

By: FYIcenter.com

(Continued from previous topic...)

How To Use "BEGIN ... END" Statement Structures?

"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

(Continued on next topic...)

  1. How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures?
  2. How To Use "BEGIN ... END" Statement Structures?
  3. How To Use WHILE Loops?
  4. How To Stop a Loop Early with BREAK Statements?
  5. How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?

Related Articles:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...