Icon BEGIN..END

Declares a block of statements.

Syntax
[Label:]
BEGIN
   [<Statement>;]
   [<Statement>;]
END [Label];

Usage
Use these statements to declare a block of statements for execution in a procedure or function.

Information The outermost BEGIN..END block for any procedure or function does not require a line termination character (;) after the END, where any BEGIN..END block within the outermost block does require a line termination character after the END.

Examples
-- This procedure produces a summary
-- of the number of albums and total album
-- purchases by genre, label, or artist

CREATE PROCEDURE Summaries(IN "SummaryType" CHAR(1) COLLATE ANSI_CI)
BEGIN
    DECLARE Result CURSOR WITH RETURN FOR Stmt;

    CASE SummaryType
    -- Genres summary
    WHEN 'G' THEN
       BEGIN
       PREPARE Stmt FROM 'SELECT Genre AS Name, COUNT(Name) AS NumAlbums,
                          SUM(PurchasePrice) AS TotalPurchases
                          FROM Albums
                          GROUP BY Genre';
       OPEN Result;
       END;
    -- Labels summary
    WHEN 'L' THEN
       BEGIN
       PREPARE Stmt FROM 'SELECT Label AS Name, COUNT(Name) AS NumAlbums,
                          SUM(PurchasePrice) AS TotalPurchases
                          FROM Albums
                          GROUP BY Label';
       OPEN Result;
       END;
    -- Artists summary
    WHEN 'A' THEN
       BEGIN
       PREPARE Stmt FROM 'SELECT Artist AS Name, COUNT(Name) AS NumAlbums,
                          SUM(PurchasePrice) AS TotalPurchases
                          FROM Albums
                          GROUP BY Artist';
       OPEN Result;
       END;
    END CASE;
END

SQL 2003 Standard Deviations
This statement deviates from the SQL 2003 standard in the following ways:

DeviationDetails
None
Image