SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

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.

SELECT * FROM vbap INTO TABLE @DATA(lt_vbap) WHERE vbeln = '4000018541'.
WRITE sy-dbcnt LEFT-JUSTIFIED.
Code

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.

DESCRIBE TABLE lt_vbap LINES DATA(lv_lines_cnt).
WRITE lv_lines_cnt.
Code

Here is our sample ABAP program which displays number of rows selected when a SELECT statement is executed on SAP database.

ABAP select statements and display number of rows selected

Here is the output of the above ABAP code.

select row count for ABAP programmer

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.

SELECT count(*) FROM vbap INTO @DATA(lv_cnt) WHERE vbeln = '4000018541'.
WRITE:/ 'sy-dbcnt', sy-dbcnt LEFT-JUSTIFIED.
WRITE:/ 'lv_cnt', lv_cnt LEFT-JUSTIFIED.
Code

number of rows in SAP database table with ABAP

When you execute the sample ABAP report, the output showing the number of rows selected as as follows.

row numbers count in SAP table



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.