Previous Topic

Next Topic

For-EndFor

Valid in: DBProc

Repeats a series of statements while a specified condition is true.

Previous Topic

Next Topic

Syntax

The For - EndFor statement has the following format:

[label:] FOR select_stmt DO

              statement; {statement;}

              ENDFOR;

Previous Topic

Next Topic

Description

The For-EndFor statement define a program loop driven by the rows retrieved by the select_stmt. These statements can only be used inside a database procedure. The select statement must have an into clause so that it can return result values into local variables in the procedure. The statement list can include any series of legal database procedure statements, including another for statement. The statement list is executed once for each row returned by the select statement. After the last row from the result set of the select statement is processed through the statement list, the for loop is terminated and execution continues with the first statement following the endfor.

The endloop statement also terminates a for loop. When endloop is encountered, the loop is immediately closed, the remaining rows in the result set of the select statement (if any) are discarded, and execution continues with the first statement following endfor. For example,

        FOR select_1 DO

        statement_list_1

        IF condition_1 THEN

        ENDLOOP;

        ENDIF;

        statement_list_2;

ENDFOR;

In this case, statement_list_1 and statement_list_2 are executed once for each row returned by select_1. As soon as condition_1 is true, statement_list_2 is not executed in that pass through the loop, select_1 is closed and the entire loop is terminated."

A for statement can be labeled. The label enables the endloop statement to break out of a nested series of for statements to a specified level. The label precedes for and is specified by a unique alphanumeric identifier followed by a colon, as in the following:

A: for...

The label must be a legal object name. The endloop statement uses the label to indicate which level of nesting to break out of. If no label is specified after endloop, only the innermost loop currently active is closed.

The following example illustrates the use of labels in nested for statements:

label_1:        FOR select_1 DO
                     statement_list_1
label_2:            FOR select_2 DO
                            statement_list_2
                            IF condition_1 THEN
                                ENDLOOP label_1;
                            ELSEIF condition_2 THEN
                                ENDLOOP label_2;
                            ENDIF;
                            statement_list_3
                    ENDFOR;
                    statement_list_4
                ENDFOR;

In this example, there are two possible breaks out of the inner loop. If condition_1 is true, both loops are closed, and control resumes at the statement following the outer loop. If condition_1 is false but condition_2 is true, the inner loop is exited and control resumes at statement_list_4.

If an error occurs during the evaluation of a for statement, the database procedure terminates and control returns to the calling application.

The For - EndFor statement has the following format:

[label:] FOR select_stmt DO
              statement; {statement;}
              ENDFOR;

Previous Topic

Next Topic

Permissions

Anyone with permission to create procedures.


© 2007 Ingres Corporation. All rights reserved.