FOREACH loop is used to apply some actions to each row of data that a cursor returns from query.
Cursor name |
The name of a previously declared cursor |
Used to specify a variable or variables used as the query criteria |
|
Used to specify variable or variables for storage of the query results |
|
A block that must include at least one executable statement. It can also include the definition block |
The FOREACH statement combines the features of the OPEN, FETCH and CLOSE statements. It retrieves and processes the rows of data that have been selected form a database. The FOREACH statement opens the specified cursor, fetches the rows selected one by one and closes the cursor after it has processed the last row.
A cursor must be declared by means of the DECLARE statement, before a FOREACH statement for this cursor can appear in the source code. If you try to FETCH a cursor that has not been declared, 4GL produces a compile-time error. Any type of cursor (SCROLL cursor, cursor FOR UPDATE or cursor WITH HOLD) can be used with the FOREACH statement, but it processes the rows one by one only in sequential order.
The FOREACH statement closes cursor when the last of the selected rows has been fetched, or if the WHENEVER NOTFOUND condition is true (if status built-in variable is equal to 100).