39

How do I get the row count of an internal table? I guess that I can loop on it. But there must be a saner way.

I don't know if it makes a difference but the code should run on 4.6c version.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Igal Serban
  • 10,558
  • 3
  • 35
  • 40

9 Answers9

97

There is also a built-in function for this task:

variable = lines( itab_name ).

Just like the "pure" ABAP syntax described by IronGoofy, the function "lines( )" writes the number of lines of table itab_name into the variable.

Community
  • 1
  • 1
user51478
  • 1,270
  • 8
  • 6
  • 8
    This one is vastly superior to `describe` due to the fact you can use as an inline operation. For instance: `If lines ( itab ) > 0. itab[ 1 ]-text = 'Potato'. endif.` or `coeff = lines( lt_relevant ) / lines( lt_all )`. – Zero Oct 24 '18 at 06:29
57

You can use the following function:

 DESCRIBE TABLE <itab-Name> LINES <variable>

After the call, variable contains the number of rows of the internal table .

Thorsten
  • 12,921
  • 17
  • 60
  • 79
14

Beside the recommended

DESCRIBE TABLE <itab-Name> LINES <variable>

there is also the system variable SY-TFILL.

From documentation:

After the statements DESCRIBE TABLE, LOOP AT and READ TABLE, the number of rows of the accessed internal table.

Example script:

REPORT ytest.

DATA pf_exclude TYPE TABLE OF sy-ucomm WITH HEADER LINE.

START-OF-SELECTION.
  APPEND '1' TO pf_exclude.
  APPEND '2' TO pf_exclude.
  APPEND '3' TO pf_exclude.
  APPEND '4' TO pf_exclude.

  WRITE: / 'sy-tfill = ', sy-tfill.

  DESCRIBE TABLE pf_exclude.
  WRITE: / 'sy-tfill = ', sy-tfill, 'after describe table'.

  sy-tfill = 0. "Reset
  READ TABLE pf_exclude INDEX 1 TRANSPORTING NO FIELDS.
  WRITE: / 'sy-tfill = ', sy-tfill, 'after read table'.


  sy-tfill = 0. "Reset
  LOOP AT pf_exclude.
    WRITE: / 'sy-tfill = ', sy-tfill, 'in loop with', pf_exclude.
    sy-tfill = 0. "Reset
  ENDLOOP.

The result:

sy-tfill =           0
sy-tfill =           4  after describe tabl
sy-tfill =           4  after read table
sy-tfill =           4  in loop with 1
sy-tfill =           0  in loop with 2
sy-tfill =           0  in loop with 3
sy-tfill =           0  in loop with 4

Please get attention of the value 0 for the 2nd entry: SY-TFILL is not updated with each step, only after the first loop.

I recommend the usage SY-TFILL only, if you need it direct after the READ(1)... If there are other commands between the READ and the usage of SY-TFILL, there is always the danger of a change of the system variable.

(1) or describe table.

knut
  • 27,320
  • 6
  • 84
  • 112
3
  DATA : V_LINES TYPE I. "declare variable
  DESCRIBE TABLE <ITAB> LINES V_LINES. "get no of rows
  WRITE:/ V_LINES. "display no of rows

Refreance: http://www.sapnuts.com/courses/core-abap/internal-table-work-area.html

0

The functional module EM_GET_NUMBER_OF_ENTRIES will also provide the row count. It takes 1 parameter - the table name.

Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
  • This function modules allows you to read the number of rows in a database table. The question was about the rows of an internal table. – Gregor Wolf Dec 05 '21 at 10:37
-1

you can also use OPEN Sql to find the number of rows using the COUNT Grouping clause and also there is system field SY-LINCT to count the lines(ROWS) of your table.

TheDean
  • 275
  • 1
  • 5
  • 16
  • 8
    Guys, an internal table ( per the question ), is not a database table. COUNT is for counting lines in a database table ( hence not relevant to the question ). – tomdemuyt Dec 03 '13 at 11:56
-2

if I understand your question correctly, you want to know the row number during a conditional loop over an internal table. You can use the system variable sy-tabix if you work with internal tables. Please refer to the ABAP documentation if you need more information (especially the chapter on internal table processing).

Example:

LOOP AT itab INTO workarea
        WHERE tablefield = value.

     WRITE: 'This is row number ', sy-tabix.

ENDLOOP.
user51478
  • 1,270
  • 8
  • 6
  • 3
    Use sy-tabix carefully, it is updated on every call to LOOP, READ TABLE... and sometimes it can have an unexpected value. – franblay Nov 25 '11 at 10:22
-2
data: vcnt(4).

clear vcnt.

LOOP at itab WHERE value = '1'.
  add 1 to vcnt.
ENDLOOP.

The answer will be 3. (vcnt = 3).

Mp0int
  • 18,172
  • 15
  • 83
  • 114
fame
  • 27
-3

I don't think there is a SAP parameter for that kind of result. Though the code below will deliver.

LOOP AT intTab.

  AT END OF value.

    result = sy-tabix.

    write result.  

  ENDAT.

ENDLOOP.
Coding Mash
  • 3,338
  • 5
  • 24
  • 45