Entering content frameAppending Summarized Lines Locate the document in its SAP Library structure

The following statement allows you to summate entries in an internal table:

COLLECT <wa> INTO <itab>.

<itab> must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P). You specify the line that you want to add in a work area that is compatible with the line type.

When the line is inserted, the system checks whether there is already a table entry that matches the key. If there is no corresponding entry already in the table, the COLLECT statement has the same effect as inserting the new line. If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

You should only use the COLLECT statement if you want to create summarized tables. If you use other statements to insert table entries, you may end up with duplicate entries.

Lines are added to internal tables as follows:

If the COLLECT statement is the first statement to fill the standard table, the system creates a temporary hash administration that identifies existing entries in the table. The hash administration is retained until another statement changes the contents of key fields or changes the sequence of the lines in the internal table. After this, the system finds existing entries using a linear search. The runtime for this operation increases in linear relation to the number of existing table entries. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.

The system uses a binary search to locate existing lines. The runtime for the operation increases logarithmically with the number of existing lines. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.

The system finds existing lines using the hash algorithm of the internal table. After the COLLECT statement, the system field SY-TABIX has the value 0, since hashed tables have no linear index.

Example

Example

DATA: BEGIN OF LINE,
        COL1(3) TYPE C,
        COL2(2) TYPE N,
        COL3    TYPE I,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE
          WITH NON-UNIQUE KEY COL1 COL2.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.

LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
ENDLOOP.

The output is:

         1

         2

         1

abc 12 10

def 34           5

The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECT statement, the first line of ITAB is modified. The following diagram shows the three steps:

This graphic is explained in the accompanying text

 

 

 

 

Leaving content frame