Try OpenEdge Now
skip to main content
ABL Essentials
Using Queries : Defining and using queries : Determining the current number of rows in a query
 

Determining the current number of rows in a query

You can use the NUM-RESULTS function to determine how many rows there are in the current results list:
NUM-RESULTS ( query-name )
This INTEGER function returns the number of rows currently in the query's results list. As with the QUERY-OFF-END function, the query-name is an expression, which can be either the quoted name of the query or a variable containing the name.
The phrase "currently in the query's results list" requires some explanation. The results list is a list of the row identifiers of all rows that satisfy the query, and that have already been retrieved.
The AVM normally builds up the results list as you walk through the query using the GET statement. Therefore, when you first open a query with a FOR EACH clause in the OPEN QUERY statement, the results list is empty and the NUM-RESULTS function returns zero.
As you move through the query using the GET NEXT statement, the AVM adds each new row's identifier to the results list and increments the value returned by NUM-RESULTS. For example, this example retrieves all the Customers in the state of Louisiana using a query. For each row, it displays the Customer Number, Name, and the value of NUM-RESULTS:
DEFINE QUERY CustQuery FOR Customer.

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"
    WITH FRAME CustFrame 15 DOWN.
  GET NEXT CustQuery.
  DOWN WITH FRAME CustFrame.
END.
When you run the procedure, you see the value of NUM-RESULTS change as each new row is retrieved, as shown in the following figure.
Figure 38. Result of NUM-RESULTS example
* Using a DOWN frame and the DOWN WITH statement
* Retrieving query results in advance