COLLECT Keyword - Introduction, Syntax & It's Importance - SAP ABAP.

Submitted By:  Shilpa Gunjan (LnT Infotech)

COLLECT Keyword/Statement is used to add or insert a new entry in internal table without creating duplicate entries.


COLLECT statement doesn't directly append the new entry to the internal table rather it first checks in the internal table for any record matching the key in the work area or the header line data.


If no matching records are found, then the new data in the work area or the header line will be inserted to the internal table. But in case if any record found with the same key, then instead of inserting a new record, it will add the numeric values of the work area or header line with the matched record and update the internal table record.


In simple terms, COLLECT keyword is used to fill an internal table without creating duplicate entries and add up the packed, integer and floating point fields at the same time.


Numeric fields include Integer (I), Packed (P) and Floating (F) data types.


In case if it’s a sorted internal table then we usually define the key, but in case of standard internal table the collection of non-numeric fields is treated as key.


Collect statement will remove the duplication while adding the records.


COLLECT statement sets SY-TABIX to the index of the existing or inserted line in the table. If the table has the type HASHED TABLE, SY-TABIX is set to 0.


You can only use the COLLECT command in a table if all of its non-key fields are numeric type (I, P, or F).


After adding rows to an internal table with COLLECT, you should avoid adding more rows with APPEND.



COLLECT KEYWORD uses a hash algorithm and is independent of the number of entries in the table (it does not need to maintain a table index). Therefore COLLECT is faster than READ BINARY combined with INSERT.


When you fill an internal table, do not use COLLECT in combination with any other table filling statements (APPEND, INSERT, MODIFY, SELECT * INTO TABLE, SELECT * APPENDING TABLE). If you mix COLLECT with other statements, COLLECT cannot use its hash algorithm and resorts to a normal linear search which is considerably slower.



SYNTAX:

COLLECT <wa_tab> INTO <it_tab>

COLLECT <it_tab>.



ALSO READ:

- COLLECT Keyword - Introduction, Syntax & It's Importance.

- Sample Program On COLLECT Keyword / Statement - 1 (Standard Internal Table).


- Sample Program On COLLECT Keyword / Statement - 2 (Performance Analysis).


- Sample Program On COLLECT Keyword / Statement - 3 (Performance Analysis).



Your suggestions and comments are welcome in this section.

Please mail all your contributions to administrator@abapmadeeasy.com We request you to mention your Name, Designation, Experience & Organization you are working for. Your posts will be verified and posted in this site with your name.