Top > Lycia reference > Querix 4GL > Statements > FOREACH
To meet your needs, we constantly work to improve Querix products.
This means that Lycia documentation is developing as well.
In case you have found a certain dissonance between the provided information and the actual behavior of Lycia 3 and/or your applications, please, let us know about this via documentation@querix.com so that we can introduce the necessary changes to our documentation.
Thank you for your attention and cooperation.
FOREACH loop is used to apply some actions to each row of data that a cursor returns from query.
Element |
Description |
Cursor Name |
The name of a previously declared cursor |
USING Clause |
Used to specify a variable or variables used as the query criteria |
INTO Clause |
Used to specify variable or variables for storage of the query results |
Logical Block |
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).