Try OpenEdge Now
skip to main content
ABL Essentials
Using Queries : Defining and using queries : Identifying the current row in the query
 

Identifying the current row in the query

As you move through the results list, the AVM keeps track of the current row number, that is, the sequence of the row in the results list. You can retrieve this value using the CURRENT-RESULT-ROW function:
CURRENT-RESULT-ROW ( query-name )
The function returns an INTEGER value with the sequence of the current row. The query-name is an expression, either a quoted query name or a variable reference.
For CURRENT-RESULT-ROW to work properly, you must define the query to be SCROLLING. If you don't define the query as SCROLLING, the CURRENT-RESULT-ROW function returns a value, but that value is not reliable.
To use CURRENT-RESULT-ROW, make these changes to your sample procedure:
DEFINE QUERY CustQuery FOR Customer SCROLLING.

OPEN QUERY CustQuery FOR EACH Customer WHERE Customer.State = "LA".
GET FIRST CustQuery.

DO WHILE NOT QUERY-OFF-END("CustQuery"):
  DISPLAY Customer.CustNum Customer.Name
    NUM-RESULTS("CustQuery") LABEL "Rows"
    CURRENT-RESULT-ROW("CustQuery") LABEL "Row#"
    WITH FRAME CustFrame 15 DOWN.
  GET NEXT CustQuery.
  DOWN WITH FRAME CustFrame.
END.
When you run the procedure, you see that the value of CURRENT-RESULT-ROW keeps pace with NUM-RESULTS, as shown in the following figure.
Figure 39. Result of CURRENT-RESULT-ROW example
This is not always the case, of course. If you use the PRESELECT option or a nonindexed sort to retrieve the data, then NUM-RESULTS is always 13, as you have seen. But the value of CURRENT-RESULT-ROW changes from 1 to 13 just as it does above.
You can use the CURRENT-RESULT-ROW function to save off a pointer to reposition to a specific row. See Usinga RowID to identify a record for information on how to identify a record.
Here are a few special cases for CURRENT-RESULT-ROW:
*If the query is empty, the function returns the Unknown value (?).
*If the query is explicitly positioned before the first row, for example by executing a GET FIRST followed by a GET PREV, then the function returns the value 1.
*If the query is explicitly positioned after the last row, for example by executing a GET LAST followed by a GET NEXT, then the function returns the value one more than the number of rows in the results list.
* Using INDEXED-REPOSITION to improve query performance
* INDEXED-REPOSITION and field lists
* Factors that invalidate CURRENT-RESULT-ROW and NUM-RESULTS