To avoid an infinite loop when updating indexed fields within a FOR EACH
1. Use another index in the FOR EACH statement
The USE-INDEX phrase to specify another index which does not contain the key field(s) that are being updated.
Selecting an inappropriate index will result in a less efficient and thus slower query.
FOR EACH Customer USE-INDEX Name EXCLUSIVE-LOCK:
ASSIGN CustNum = CustNum + 5000.
/* CustNum is not part of the Name index, which results in no looping on that index */
END.
2. Use a PRESELECT loop instead of the FOR EACH statementPRESELECT will build a fixed result list up front and then process the records in that list only once.
DEFINE QUERY NoInfiniteLoopsWanted FOR Customer.
OPEN QUERY NoInfiniteLoopsWanted PRESELECT EACH Customer.
GET FIRST NoInfiniteLoopsWanted.
REPEAT WHILE QUERY-OFF-END("NoInfiniteLoopsWanted") = FALSE:
ASSIGN CustNum = CustNum + 5000.
GET NEXT NoInfiniteLoopsWanted.
END.
CLOSE QUERY NoInfiniteLoopsWanted.
Use a REPEAT PRESELECT loop:
REPEAT PRESELECT Customer EXCLUSIVE-LOCK:
FIND NEXT Customer.
ASSIGN CustNum = CustNum + 5000.
END.