user11909
user11909

Reputation: 1265

How to use BY keyword correctly in FOR EACH

I want to go through the records in descending order by Field4, but when I put in the code BY mybuffer.Field4 DESCENDING I get the error

After "Field4 DESCENDING" not understandable (247) [translated].

FOR EACH mybuffer 
    WHERE mybuffer.Field1 = value1
    AND   mybuffer.Field2 = value2
    AND   mybuffer.Field3 >= value3
    BY    mybuffer.Field4 DESCENDING
    USE-INDEX myindex
    NO-LOCK:

    /* do something */
END.

What I am doing wrong?

Upvotes: 1

Views: 1521

Answers (2)

Tom Bascom
Tom Bascom

Reputation: 14020

The error is the placement of USE-INDEX "After field 4 descending". Just like the error message says.

The correct syntax is:

for each customer no-lock use-index cust-num
  where sales-rep = "BBB"
  by city:

  display customer.

end.

FWIW it is unlikely to be a good idea to have both USE-INDEX in a FOR EACH loop and even less likely to be a good idea to be combining it with BY. The compiler will probably choose a better index (or indexes plural) if you allow it to.

Upvotes: 1

Mike Fechner
Mike Fechner

Reputation: 7192

Although I'm not sure that the combination of USE-INDEX and BY is smart. But your problem is likely to be the order of options.

Use in this order:

  • USE-INDEX
  • NO-LOCK
  • BY

Upvotes: 1

Related Questions