Try OpenEdge Now
skip to main content
ABL Essentials
Procedure Blocks and Data Access : Language statements that define blocks : FOR blocks : Using the LEAVE statement to leave a block
 
Using the LEAVE statement to leave a block
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's 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:
FOR EACH Customer NO-LOCK WHERE Customer.State = "NH" WITH FRAME f:
  DISPLAY Customer.CustNum Customer.Name.
  FOR EACH Order OF Customer NO-LOCK BY Order.OrderDate:
    DISPLAY Order.OrderNum Order.OrderDate WITH FRAME f.
    LEAVE.
  END. /* FOR EACH Order */
END. /* FOR EACH Customer */
This code uses nested blocks to retrieve the Customers and Orders separately. These nested blocks allow you to sort the Orders for a single CustomerBYOrderDate. 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.