SELECT COUNT(*) AS NumberOfRecords FROM tableX;
how do I convert this to sap ABAP

- 11,934
- 5
- 22
- 48

- 11
- 1
- 1
- 2
-
...without bothering to even use Google to search for an answer? – vwegert Dec 28 '16 at 19:04
5 Answers
For database tables you can do SELECT COUNT
like this:
SELECT COUNT( * )
INTO numberOfRecords
FROM tableX.
To get the line count of an internal table you need the DESCRIBE
statement:
DESCRIBE TABLE tableX LINES numberOfRecords.

- 680
- 1
- 14
- 27
-
-
It works both with and without `SINGLE` on our system. The F1 help doesn't show `SINGLE` for aggregate functions – maillard Dec 24 '16 at 10:33
For internal tables you can use this build in function as well:
numberOfRecords = lines( tableX)

- 172
- 8
I had a similar issue, and a colleague gave me to following. I needed the counts of entries in a db table for a key, and didn'enter code heret want to do a select count(*) in a loop. So, we created a sorted table with the unique key, did a select... endselect into that table with the condition BEFORE the loop, and in the loop simply read this new sorted table:
types:
begin of ts_tab
f1 type 1
f2 type 2
f3 type i
end of ts_tab
tt_tab type sorted table of ts_tab
with unigue key f1 f2
select f1 f2 into coorresponding fields of ls_tab
read table lt_tab assigning <ls_tab>
with table key f1 = f1 etc.
if sy-subrc = 0.
add 1 to <ls_tab>-f3
else.
ls_tab-f3 = 1.
insert ls_tab into lt_tab.
endif.
endselect
loop etc.
read lt_tab into ls_tab with key f1 = f1 f2 = f2.
if sy-subrc eq 0.
my_count = ls_tabl-f3.
endif
endloop

- 1
You may use ABAP Statement:
DESCRIBE TABLE itab[] lines lv_no.

- 374,368
- 89
- 403
- 331
1) If you just want the count of the records in database table, use the following syntax.
SELECT COUNT( * ) INTO RecordCount FROM tableX.
2) But, if you need the records for processing, as well as the count then use following.
SELECT * INTO TABLE itab FROM tableX.
DESCRIBE TABLE itab[] lines RecordCount.
-
-
Hardly any. I believe the question was about SQL table, not the internal tables. If it was about internal tables then `lines( )` is more suitable. – Dharman Aug 15 '19 at 16:01
-
The difference i am pointing to is in this line. SELECT * INTO TABLE itab FROM tableX. Anyway, i later realized it was an old post. So not much use in arguing among us. – M Shaik Aug 16 '19 at 11:35