21. 4GL Statement Glossary : Select : Using a Select Loop
 
Share this page                  
Using a Select Loop
The select loop allows the program to perform a computation for each selected row, rather than to display results to you, as in the select to a form, table field, record, or array statement. You can use a select loop to select into a field or variable, a table field, record, an array, or the attributes of a record or array.
A select loop selects and assigns multiple rows of data, one row at a time. A series of specified operations is performed once on the values in each row selected. The syntax for a select loop is as follows:
[repeated] select [distinct] 
  [ [:]simpleobjectname =] expression |
  [tablename.]columnname
  {, [ [:]simpleobjectname =] expression |
  [tablename.]columnname }
from tablename [corrname] {, tablename [corrname] }
[where qual
[group by columnname {, columnname} [having qual] ] 
[order by orderfieldname [sortorder
    {, orderfieldname [sortorder] } ]
begin | { 
  statementlist
end | }
This form of the statement executes the commands in the statementlist once for each row returned by the query.
The value selected from the right-hand side of the target list element is assigned to the variable in the left-hand side of the target list element.
The left-hand side, if present, must be a legal 4GL variable reference. This variable can be a simple field or local or global variable of an Ingres data type, a table field cell, or a record attribute. This allows values to be assigned directly into these fields, simple variables, or records.
If the left-hand side is not present, the right-hand side must be a database table.column reference, in which case the column's name must be the same as a simple field or local variable to which the value is assigned.
During execution of the loop, the value of inquire_sql (rowcount) is not defined; if a query statement is executed within the loop, then rowcount is defined for that statement. After the select loop, rowcount equals the number of rows through which the program looped.
Select loops perform an implicit next operation each time the loop is executed. In addition, an explicit next statement within the body of the loop causes the next row of the data to be selected. Statement execution continues with the statement immediately following next. The loop terminates at the end of the data set. Use the endloop statement to terminate the loop early. For more information, see Endloop.