Progress
Language Reference
FOR Statement
Starts an iterating block that reads a record from each of one or more tables at the start of each block iteration.
DATA MOVEMENT
![]()
BLOCK PROPERTIES
Iteration, record reading, record scoping, frame scoping, transactions by default.
SYNTAX
EACH
Starts an iterating block, finding a single record on each iteration. If you do not use the EACH keyword, the Record phrase you use must identify exactly one record in the table.
FIRST
Uses the criteria in the record-phrase to find the first record in the table that meets that criteria. Progress finds the first record before any sorting.
The procedure above displays customer 1 (cust-num is the primary index of the customer table), not the customer with the lowest credit-limit. A procedure that displays the customer with the lowest credit-limit looks like the following.
See the NOTES section for more information on using this option.
LAST
Uses the criteria in the record-phrase to find the last record in the table that meets that criteria. Progress finds the last record before sorting.
The procedure above displays the customer with the highest customer number (cust-num is the primary index of the customer table), not the customer with the highest credit-limit.
A procedure that displays the customer with the highest credit-limit looks like the following.
See the NOTES section for more information on using this option.
record-phrase
Identifies the set of records you want to retrieve. This can also be the built-in buffer name, proc-text-buffer, that you can use to return table rows from a stored procedure.
To use FOR EACH/FIRST/LAST to access a record in a table defined for multiple databases, you must qualify the record’s table name with the database name.
This is the syntax for record-phrase.
Specifying multiple occurrences of record-phrase selects the tables using an inner join.
For more information on record-phrase and inner joins, see the Record Phrase reference entry.
query-tuning-phrase
Allows programmatic control over the execution of a DataServer query.
For more information on the query-tuning-phrase, see the Progress DataServer Guides, Progress DataServer for ODBC Guide and Progress DataServer for ORACLE Guide.
BREAK
Over a series of block iterations, you might want to do some work based on whether the value of a certain field changes. This field defines a break group. For example, you might be accumulating some value, such as a total. You use the BREAK option to define state as the break group.
Here, Progress accumulates the total credit-limit for all the customers in the customer table. Each time the value of the state field changes, Progress displays a subtotal of the credit-limit values for customers in that state.
You can use the BREAK option anywhere in the block header, but you must also use the BY option to name a sort field.
You can use the BREAK option in conjunction with the ACCUMULATE Statement and ACCUM Function. For more information, see the reference entries for those language elements.
BY expression [ DESCENDING ]
Sorts the selected records by the value of expression. If you do not use the BY option, Progress retrieves records in the order of the index used to satisfy the record-phrase criteria, or the primary index if no criteria is given. The DESCENDING option sorts the records in descending order (not in default ascending order).
You can use multiple BY options to do multi-level sorting.
Here, the customers are sorted in order by credit-limit. Within each credit-limit value, customers are sorted alphabetically by name.
There is a performance benefit if an index on expression exists: BREAK BY does not have to perform the sort that is otherwise required to evaluate FIRST, LAST, FIRST-OF, and LAST-OF expressions.
string
A CHARACTER expression that evaluates to the string whose collation value you want to compute.
strength
A CHARACTER expression that evaluates to one of the following:
- RAW - Causes COLLATE to compute the collation value of the string from its binary value.
- CASE-SENSITIVE - Causes COLLATE to compute the case-sensitive collation value of the string using a particular collation table.
- CASE-INSENSITIVE - Causes COLLATE to compute the case-insensitive collation value of the string using a particular collation table.
collation
A CHARACTER expression that evaluates to the name of a collation table.
If collation does not appear, COLLATE uses the collation table of the client.
Progress reports an error and stops execution if one of the following occurs:
variable = expression1 TO expression2 [ BY k ]
Identifies the name of a field or variable whose value you are incrementing in a loop. The expression1 is the starting value for variable on the first iteration of the loop. The k is the amount to add to variable after each iteration and must be a constant. It (k) defaults to 1. The variable, expression1, and expression2 parameters must be integers.
When variable exceeds expression2 (or is less than expression2 if k is negative) the loop ends. Since expression1 is compared to expression2 at the start of the first iteration of the block, the block can be executed 0 times. Progress re-evaluates expression2 on each iteration of the block.
WHILE expression
Indicates the condition in which you want the FOR EACH block to continue processing the statements within it. Using the WHILE expression option causes the block to iterate as long as the condition specified by the expression is TRUE or Progress reaches the end of the index it is scanning, whichever comes first. The expression is any combination of constants, operators, field names, and variable names that yield a logical value.
TRANSACTION
Identifies the FOR EACH block as a system transaction block. Progress starts a system transaction for each iteration of a transaction block if there is not already an active system transaction. See the Progress Programming Handbook for more information on transactions.
on-error-phrase
Describes the processing that takes place when there is an error during a block. This is the syntax for the ON ERROR Phrase.
For more information, see the ON ERROR Phrase reference entry.
on-endkey-phrase
Describes the processing that takes place when the ENDKEY condition occurs during a block. This is the syntax for the ON ENDKEY phrase.
For more information, see the ON ENDKEY Phrase reference entry.
on-quit-phrase
Describes the processing that takes place when a QUIT statement is executed during a block. This is the syntax for the ON QUIT Phrase.
For more information, see the ON QUIT Phrase reference entry.
on-stop-phrase
Describes the processing that takes place when the STOP conditions occurs during a block. This is the syntax for the ON STOP Phrase.
For more information, see the ON STOP Phrase reference entry.
frame-phrase
Specifies the overall layout and processing properties of a frame. For more information on frame-phrase, see the Frame Phrase reference entry.
EXAMPLESThis procedure reads customer records that have a cust-num less than 12, sorting the records in order by state before displaying them.
The next procedure gets information from four related tables (customer, order, order-line, and item) and displays some information from each. Before displaying the information, the FOR EACH statement sorts it in order by the promise-date field, then, within that field, in order by cust-num. Within the cust-num field, the data is sorted by the line-num field.
This procedure uses the LAST option to display information on the last order of each customer.
NOTES
- At compile time, Progress determines which index or indexes to use for retrieving records from a table, based on the conditions in the Record phrase. For compatibility with Progress Version 6 or earlier, you can force Progress to use only one index by specifying the USE-INDEX option or by using the Version 6 Query (-v6q) parameter.
- If you specify the -v6q startup parameter, an index component is involved in an equality match if it is used in the Record phrase conditions in the following form:
Where the expression is independent of any fields in the table that the index is being selected from. A condition involving OF and USING are equivalent to this form. A field is involved in a range match if it is used in a condition of this form.
NOTE: The BEGINS operator translates into two range matches for a field.
An equality or range match is considered active if the equality or range condition stands on its own or is related to other conditions solely through the AND operator (for example, not through OR or NOT).
A field is involved in a sort match if it is used in a BY option of this form.
- If you specify the -v6q startup parameter, the following list describes the rules the Progress database manager uses to choose an index for a Progress database:
- If you specify the record by ROWID, Progress accesses the record directly without using an index.
- If you use the USE-INDEX option, in the record-phrase, Progress uses the index you name in that option.
- For each index in the table, Progress looks at each index component in turn and counts the number of active equality, range, and sort matches. Progress ignores the counts for any components of an index that occur after a component that has no active equality match. Progress compares the results of this count and selects the best index. Progress uses the following order to determine the better of any two indexes.
- If one index is unique and all of its components are involved in active equality matches and the other index is not unique, or if not all of its components are involved in active equality matches, Progress chooses the former of the two.
- Select the index with more active equality matches.
- Select the index with more active range matches.
- Select the index with more active sort matches.
- Select the index that is the primary index.
- Select the first index alphabetically by index name.
- If you specify the -v6q startup parameter, Progress might have to scan all the records in the index to find those meeting the conditions, or Progress might have to examine only a subset of the records. This latter case is called bracketing the index and results in more efficient access. Having selected an index as previously described, Progress examines each component as follows to see if the index can be bracketed:
- If the component has an active equality match, Progress can bracket it, and it examines the next component for possible bracketing.
- If the component has an active range match, Progress can bracket it,but it does not examine the remaining components for possible bracketing.
- If the component does not have an active equality match or an active range match, Progress does not examine the remaining components for bracketing.
- If you specify the v6q parameter, any conditions you specify in the record-phrase that are not involved in bracketing the selected index are applied to the fields in the record itself to determine if the record meets the overall record-phrase criteria. For example, assume that the f table has fields a, b, and c involved in two indexes:
- Primary, unique index (I1) on a, b, and c.
- Secondary non-unique index (I2) on c.
Table 26 shows the index Progress selects and the bracketed part of the index for various record-phrases.
Table 26: Progress Version 6 Index Selection Examples Record Phrase Index
Selected Bracketing On f WHERE a = 3 AND b = 2 AND c = 3 I1 a + b + c f WHERE a = 3 I1 a f WHERE c = 1 I2 c f WHERE a = 3 AND b > 7 AND c = 3 I1 a + b f WHERE a = 3 AND c = 4 I1 a f WHERE b = 5 I1 None of the fields1 f WHERE a = 1 OR b >5 I1 None of the fields1 f WHERE (a >= a1 AND a <= a2)
OR (a1=0) I1 None of the fields2 f WHERE a >= (IF a1 NE 0 THEN a1
ELSE -99999999) AND
a <= (IF a1 NE 0 THEN a2
ELSE +99999999) I1 a2- The FIRST and LAST keywords are especially useful when you are sorting records in a table in which you want to display information. Often, several related records exist in a related table, but you only want to display the first or last related record from that table in the sort. You can use FIRST or LAST in these cases. Two examples follow.
Suppose you were interested in displaying the date when each customer first placed an order. This procedure displays the customer number and date of the first order.
The following procedure displays the last order line of every order, sorted by the price of the item and by the promised date of the order.
- If you want Progress to use a specific index, you must specify the first component of that index in the record phrase of the FOR statement.
- For more information on the FOR statement, see the Progress Programming Handbook .
- SpeedScript — The on-endkey-phrase and the on-quit-phrase do not apply.
- The COLLATE option computes the collation value of a string after applying a particular “strength” (RAW, CASE-SENSITIVE, or CASE-INSENSITIVE) and, optionally, a particular collation table.
SEE ALSO
FIND Statement, Frame Phrase, ON ENDKEY Phrase, ON ERROR Phrase, ON QUIT Phrase, ON STOP Phrase, Record Phrase
Copyright © 2004 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |