-2

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

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
your friend
  • 11
  • 1
  • 1
  • 2

5 Answers5

5

For database tables you can do SELECT COUNTlike 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. 
maillard
  • 680
  • 1
  • 14
  • 27
  • Don't you have to add `SINGLE` to use this `SELECT` without `ENDSELECT`? –  Dec 24 '16 at 10:02
  • 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
1

For internal tables you can use this build in function as well:

numberOfRecords = lines( tableX)
Togo
  • 172
  • 8
0

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
ShaunA
  • 1
-1

You may use ABAP Statement:

DESCRIBE TABLE itab[] lines lv_no.
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
-2

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.
Dharman
  • 30,962
  • 25
  • 85
  • 135
M Shaik
  • 1
  • 1
  • There is a difference in the syntax of the Case 2. – M Shaik Aug 15 '19 at 15:41
  • 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