PreviousNextIndex

FOR statement

Starts an iterating block that reads a record from each of one or more tables at the start of each block iteration. Use an END statement to end a FOR block.

Data movement

Block properties

Iteration, record reading, record scoping, frame scoping, transactions by default.

Syntax

[ label: ] 
FOR [ EACH | FIRST | LAST ] record-phrase 
  [ , [ EACH | FIRST | LAST ] record-phrase ] ... 
  [ query-tuning-phrase ] 
  [ BREAK ] 
  [ BY expression [ DESCENDING ]  
  | COLLATE ( string , strength [ , collation ] ) [ DESCENDING ] 
  ] ... 
  [ variable = expression1 TO expression2 [ BY k ] ] 
  [ WHILE expression ] 
  [ TRANSACTION ] 
  [ STOP-AFTER expression ] 
  [ on-error-phrase ] 
  [ on-endkey-phrase ] 
  [ on-quit-phrase ] 
  [ on-stop-phrase ] 
  [ frame-phrase ] : 
  for-body 

EACH
FIRST
LAST
record-phrase
query-tuning-phrase
BREAK
BY expression [ DESCENDING ]
COLLATE ( string , strength [ , collation ] ) [ DESCENDING ]
string
strength
collation
variable = expression1 TO expression2 [ BY k ]
WHILE expression
TRANSACTION
STOP-AFTER expression
on-error-phrase
on-endkey-phrase
on-quit-phrase
on-stop-phrase
frame-phrase
for-body
for-logic
catch-block
finally-block
END
Examples

This procedure reads Customer records that have a CustNum less than 12, sorting the records in order by State before displaying them:

r-fore.p
FOR EACH Customer NO-LOCK WHERE Customer.CustNum < 12 BY Customer.State: 
  DISPLAY Customer.CustNum Customer.Name Customer.City Customer.State. 
END. 

The next procedure gets information from four related tables (Customer, Order, OrderLine, and Item) and displays some information from each. Before displaying the information, the FOR EACH statement sorts it in order by the PromiseDate field, then, within that field, in order by CustNum. Within the CustNum field, the data is sorted by the LineNum field.

r-fore2.p
FOR EACH Customer NO-LOCK, EACH Order OF Customer NO-LOCK, 
  EACH OrderLine OF Order NO-LOCK, Item OF OrderLine NO-LOCK 
  BY Order.PromiseDate BY Customer.CustNum BY OrderLine.LineNum: 
  DISPLAY Order.PromiseDate Customer.CustNum Order.OrderNum 
    OrderLine.LineNum Item.ItemNum Item.ItemName. 
END. 

This procedure uses the LAST option to display information on the last Order of each Customer:

r-fore3.p
FOR EACH Customer NO-LOCK, LAST Order OF Customer: 
  DISPLAY Customer.CustNum Customer.Name Order.OrderNum Order.OrderDate 
    Order.Instructions. 
  PAUSE 1 NO-MESSAGE. 
  Order.Instructions = "Last order". 
  DISPLAY Order.Instruction. 
END. 

Notes
See also

CATCH statement, FINALLY statement, FIND statement, Frame phrase, ON ENDKEY phrase, ON ERROR phrase, ON QUIT phrase, ON STOP phrase, Record phrase


OpenEdge Release 10.2B
Copyright © 2009 Progress Software Corporation
PreviousNextIndex