FOR statement

The FOR statement executes a statement or group of statements for each row of a table.

Syntax

Read syntax diagramSkip visual syntax diagramlabel:FORfor-loop-nameAScursor-nameCURSORWITHOUT HOLDWITH HOLDFORselect-statementDOSQL-procedure-statement ; END FOR label

Description

label
Specifies the label for the FOR statement. If the ending label is specified, it must be the same as the beginning label. The label name cannot be the same as the routine name or another label within the same scope. For more information, see References to SQL labels.
for-loop-name
Specifies the label for the implicit compound-statement that is generated to implement the FOR statement. It follows the rules for the label of a compound-statement except that it cannot be used with an ITERATE, GOTO, or LEAVE statement within the FOR statement. The for-loop-name is used to qualify the column names returned by the specified select-statement. It must not be the same as any label within the same scope. For more information, see References to SQL labels.

Either the for-loop-name or label can be used to qualify other SQL variable names in the statement.

If for-loop-name is specified, then it should be used to qualify any other SQL variable names in the statement when debugging the SQL function, SQL procedure, or SQL trigger.

cursor-name
Names a cursor.
WITH HOLD
Prevents the cursor from being closed as a consequence of a commit operation. A cursor declared using the WITH HOLD clause is implicitly closed at commit time only if the connection associated with the cursor is ended during the commit operation. For more information, see DECLARE CURSOR.
select-statement
Specifies the select statement of the cursor.

Each expression in the select list must have a name. If an expression is not a simple column name, the AS clause must be used to name the expression. If the AS clause is specified, that name is used for the variable and must be unique.

SQL-procedure-statement
Specifies the SQL statements to be executed for each row of the result table of the cursor. The SQL statements should not include an OPEN, FETCH, or CLOSE specifying the cursor name of the FOR statement.

Notes

FOR statement rules: The FOR statement executes one or multiple statements for each row in a result table of the cursor. The cursor is defined by specifying a select list that describes the columns and rows selected. The statements within the FOR statement are executed for each row selected.

The select list must consist of unique column names and the objects referenced in the select-statement must exist when the function, procedure, or trigger is created.

The cursor specified in a FOR statement cannot be referenced outside the FOR statement and cannot be specified on an OPEN, FETCH, or CLOSE statement.

Handler warning: Handlers may be used to handle errors that might occur on the open of the cursor or fetch of a row using the cursor in the FOR statement. Handlers defined to handle these open or fetch conditions should not be CONTINUE handlers as they may cause the FOR statement to loop indefinitely.

Example

In this example, the FOR statement is used to specify a cursor that selects 3 columns from the employee table. For every row selected, SQL variable fullname is set to the last name followed by a comma, the first name, a blank, and the middle initial. Each value for fullname is inserted into table TNAMES.

     BEGIN
       DECLARE fullname CHAR(40);
       FOR vl AS
           c1 CURSOR FOR
         SELECT firstnme, midinit, lastname FROM employee
          DO
            SET fullname =
               lastname || ', ' || firstnme ||' ' || midinit;
            INSERT INTO TNAMES VALUES ( fullname );
          END FOR;
     END;