COLLECT ( ABAP keyword) introduction & details
COLLECT [wa INTO] itab.
… SORTED BY
COLLECT is used to create unique or compressed datsets. The
key fields are the default key fields of the internal table itab .
use only COLLECT to fill an internal table, COLLECT makes sure that the internal
table does not contain two entries with the same default key fields.
besides its default key fields, the internal table contains number fields (see
also ABAP/4 number types ), the contents of these number fields are added
together if the internal table already contains an entry with the same key
If the default key of an internal table processed with COLLECT is
blank, all the values are added up in the first table line.
specify wa INTO , the entry to be processed is taken from the explicitly
specified work area wa . If not, it comes from the header line of the internal
table itab .
After COLLECT , the system field SY-TABIX contains the index
of the – existing or new – table entry with default key fields which match those
of the entry to be processed.
COLLECT can create unique or
compressed datasets and should be used precisely for this purpose. If uniqueness
or compression are unimportant, or two values with identical default key field
values could not possibly occur in your particular task, you should use APPEND
instead. However, for a unique or compressed dataset which is also efficient,
COLLECT is the statement to use.
If you process a table with COLLECT , you
should also use COLLECT to fill it. Only by doing this can you guarantee
the internal table will actually be unique or compressed, as
described above and
COLLECT will run very efficiently.
If you use
COLLECT with an explicitly specified work area, it must be compatible with the
line type of the internal table.
Compressed sales figures for
DATA: BEGIN OF COMPANIES OCCURS
SALES TYPE I,
END OF COMPANIES.
‘Duck’. COMPANIES-SALES = 10.
COMPANIES-NAME = ‘Tiger’.
COMPANIES-SALES = 20.
COMPANIES-NAME = ‘Duck’.
COMPANIES-SALES = 30.
The table COMPANIES now
has the following appearance:
… SORTED BY f
SORTED BY f is obsolete and should no longer be used. Use APPEND … SORTED BY f
which has the same meaning.
The cost of a
COLLECT in terms of performance increases with the width of the default key
needed in the search for table entries and the number of numeric fields with
values which have to be added up, if an entry is found in the internal table to
match the default key fields.
If no such entry is found, the cost is reduced
to that required to append a new entry to the end of the table.
statement used on a table which is 100 bytes wide and has a key which is 60
bytes wide and seven numeric fields is about approx. 50 msn (standardized
COLLECT_OVERFLOW : Overflow
in integer field when calculating totals.
Overflow in type P field when calculating totals.
Related APPEND , WRITE
… TO , MODIFY , INSERT
[ FREE ] SUBSCRIBE
RECEIVE UPDATES & SAP ARTICLES RIGHT IN YOUR INBOX.