Use the LEAVE statement to leave a block
- Last Updated: October 14, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
Use the USE-INDEX phrase only when necessary.
The AVM is extremely effective at choosing the right index, or combination of multiple
indexes, to optimize your data retrieval. In fact, there is an alternative even in the present
example that yields the same result without requiring you to know the names and fields in the
Order table's indexes. Take a look at this procedure:
|
This code uses nested blocks to retrieve the Customers and
Orders separately. These nested blocks allow you to sort the
Orders for a single Customer BY OrderDate.
You have to define the set of all the Customer's Orders
using the FOR EACH phrase so that the BY phrase has the effect of sorting them by OrderDate. But you
really only want to see the first one. To do this, you use another one-word ABL statement:
LEAVE. The LEAVE statement
does exactly what you would expect it to: It leaves the block (specifically the innermost
iterating block to the LEAVE statement) after displaying
fields from the first of the Customer's Orders. It does not
execute any more statements that might be in the block nor does it loop through any more
records that are in its result set. Instead, it moves back to the outer block to retrieve the
next Customer.
Because the LEAVE statement looks for an iterating block
to leave, it always leaves a FOR block. It leaves
a DO block only if the DO statement
has a qualifier, such as WHILE, that causes it
to iterate. If there is no iterating block, the AVM leaves the entire
procedure.