Icon LOOP

Provides an unconditional looping construct.

Syntax
[Label:]
LOOP
   <StatementBlock>
END LOOP [Label];

<StatementBlock> =

<StatementBlock> =

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

Usage
Use this statement to loop unconditionally on a single statement or multiple statements. You can use the LEAVE statement to exit the loop at any time, and the ITERATE statement to jump to the top of the loop at any time.

Examples
-- This procedure loops through
-- the Customers table and exits the loop
-- when the EOF is reached on the cursor

CREATE PROCEDURE LoopCustomers()
BEGIN
   DECLARE CustCursor SENSITIVE CURSOR FOR Stmt;

   PREPARE Stmt FROM 'SELECT * FROM Customer';

   OPEN CustCursor;

   FETCH FIRST FROM CustCursor;

   LOOP
      IF EOF(CustCursor) THEN
         LEAVE;
      ELSE
         FETCH NEXT FROM CustCursor;
      END IF;
   END LOOP;
END

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

DeviationDetails
None
Image