Find Number of Rows Selected in ABAP SELECT Statement
ABAP programmer and developers use system field sy-dbcnt to find number of rows selected after SQL SELECT statement is executed on an SAP database table. In this tutorial, developers can find samples showing how to get rows count in ABAP programs with sy-dbcnt and Describe Table command.
Following ABAP code dynamically selects data from VBAP Sales documents table for given filtering data and stores the row data into an internal table.
Right after SELECT statement, in sy-dbcnt system field the number of database rows processed is returned back.
The description of the sy-dbcnt ABAP system field is explained as "Edited Database Table Rows"
Actually if you have already stored a set of rows in an ABAP internal table, ABAP developer can read the number of rows using Describe Table command as follows.
Here is our sample ABAP program which displays number of rows selected when a SELECT statement is executed on SAP database.
Here is the output of the above ABAP code.
If ABAP developers doesn't need the data from SAP database table, but is only dealing with the number of table rows matching given filter criteria, below ABAP code can be better to use for performance.
ABAP programmer can use SQL aggregate function COUNT() and store the returned value into a local variable.
Besides local variable, the ABAP system field sy-dbcnt also keeps the number of rows affected after SELECT statement is executed.
When you execute the sample ABAP report, the output showing the number of rows selected as as follows.