id="forstmt">FOR statement

The FOR statement executes a statement for each row of a table.

 

Syntax


Click to skip syntax diagram


Read syntax diagram

Skip visual syntax diagram>>-+--------+--FOR--+-------------------+--AS-------------------> '-label:-' '-SQL-variable-name-' >----cursor-name--CURSOR--+-----------+--FOR--------------------> '-WITH HOLD-' .-----------------------------. V | >--select-statement--DO----SQL-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 Labels.

SQL-variable-name

The SQL-variable-name can be used to qualify variables in the statement. The SQL-variable-name must not be the same as any label within the same scope. For more information, see Labels.

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

If SQL-variable-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. If not specified, a unique cursor name is generated.

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 table. 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 table. 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 table specified in the select list 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;
 


[ Top of Page | Previous Page | Next Page | Contents |
Index ]