Thiru
Thiru

Reputation: 251

How do I Understand the difference between FOR EACH and FOR FIRST?

I am new to progress and I would like to know what's the difference between FOR EACH and FOR FIRST. I tried to know but don't get understandable and acceptable reason. I have written a small query and got same result.Let me share

DEFINE TEMP-TABLE tt_data
    FIELD womf AS CHARACTER
    FIELD pomf AS CHARACTER.

CREATE tt_data.
ASSIGN
    tt_data.womf = "BlockCar"
    tt_data.pomf = "Whitecar".


FOR EACH tt_data.
    DISPLAY tt_data.womf.
END.

FOR FIRST tt_data.
    DISPLAY tt_data.womf.
END.

if my query is wrong then please give me an example to understand the difference please. I need to use FOR FIRST.

Upvotes: 0

Views: 462

Answers (2)

Tom Bascom
Tom Bascom

Reputation: 14020

As Mike says, when there is just one record (like in your sample code) there is no difference.

When there are multiple records FOR EACH processes the whole set as defined by the WHERE clause (if it is present) and sorted by the BY clause (if it is present).

FOR FIRST can also have a WHERE and a BY. However -- FOR FIRST only ever works with ONE record. This can be extremely confusing if you specify sorting that differs from the index that the WHERE clause results in (or the lack thereof defaults to). The FIRST (and only) record returned by the query will be "sorted". In other words the BY has no impact on selection.

The following sports2000 example makes this clear:

for each customer no-lock where custNum > 10 by name:
  display custNum name.
end.
pause.

for first customer no-lock where custNum > 10 by name:
  display custNum name.
end.

Using FOR FIRST is almost always a mistake and a bug waiting to happen. Doubly so if a BY clause is specified.

Upvotes: 3

Mike Fechner
Mike Fechner

Reputation: 7192

There's no difference when there's just a single record.

When there are multiple records, FOR EACH processes every record, one after the other.

FOR FIRST would still only process a single record.

Upvotes: 3

Related Questions