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 SAP ABAP Programming and HANA Database Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




CRM Companies List
Web Based CRM Software


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.

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.

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.

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 Tutorials

SAP Tutorial

SAP Forums

SAP Tools

SAP Transaction Codes Table






Related Articles and Tutorials(http://www.kodyaz.com/sap-abap/find-number-of-rows-in-abap-select-statement.aspx)
How to Find Internal Table Row Count in ABAP
ABAP Tutorial - SAP Split and ABAP Split String Function
ABAP Tutorial - ABAP String Split Example Code
ABAP RegEx for ABAP Regular Expression to fetch Date from String
Update Multiple Variables in ABAP Select Single Statement
Check Existence using ABAP SELECT
Delete All Data from SAP Database Table
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Switch ABAP Variable Value with Translate using Mask
Switch ABAP Variable Value with Translate using Mask
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement


Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems