Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

COLLECT ?

Former Member
0 Kudos

what is a COLLECT ?y r u using this ?

1 ACCEPTED SOLUTION

varma_narayana
Active Contributor
0 Kudos

Hi..

COLLECT is used to Summarize the Data in internal table while adding the rows.

Collect <wa> into <itab>.

This statement compares the Non-numeric(Type C,N,D,T,X,String) fields of the work area with the Existing rows in the internal table. that means all the Non-numeric fields will act as key (For Eg Matno, Plant)

If a row is found with the same key:

It will add the Numeric fields instead of creating a new row.

If a row is not found with the same key:

It will create a new row like Append.

DATA : BEGIN OF ITAB1 OCCURS 0,

MATNR TYPE MARD-MATNR,

WERKS TYPE MARD-WERKS,

LABST TYPE MARD-LABST,

END OF ITAB.

DATA :WA LIKE ITAB1.

DATA: ITAB2 LIKE ITAB1 OCCURS 0.

SELECT MATNR WERKS LABST FROM MARD INTO TABLE ITAB1.

LOOP AT ITAB1 INTO WA.

COLLECT WA INTO ITAB2.

ENDLOOP.

Check the contents of both ITAB1 AND ITAB2.

<b>Reward if Helpful.</b>

6 REPLIES 6

Sougata
Active Contributor
0 Kudos

Put your cursor on COLLECT and hit the F1 key...its explained well in the ABAP Keyword Documentation.

Former Member
0 Kudos

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.

Regards

Siddhartha

Former Member
0 Kudos

hi

collect works like append but the difference between collect and append is

append : add record from header to body part of the internal table

collect : adds record from header to body part of the internal table but if there is already a record exits with same entry then the integer value is added to that existing value .

Reward if useful,

Srinivas.D

varma_narayana
Active Contributor
0 Kudos

Hi..

COLLECT is used to Summarize the Data in internal table while adding the rows.

Collect <wa> into <itab>.

This statement compares the Non-numeric(Type C,N,D,T,X,String) fields of the work area with the Existing rows in the internal table. that means all the Non-numeric fields will act as key (For Eg Matno, Plant)

If a row is found with the same key:

It will add the Numeric fields instead of creating a new row.

If a row is not found with the same key:

It will create a new row like Append.

DATA : BEGIN OF ITAB1 OCCURS 0,

MATNR TYPE MARD-MATNR,

WERKS TYPE MARD-WERKS,

LABST TYPE MARD-LABST,

END OF ITAB.

DATA :WA LIKE ITAB1.

DATA: ITAB2 LIKE ITAB1 OCCURS 0.

SELECT MATNR WERKS LABST FROM MARD INTO TABLE ITAB1.

LOOP AT ITAB1 INTO WA.

COLLECT WA INTO ITAB2.

ENDLOOP.

Check the contents of both ITAB1 AND ITAB2.

<b>Reward if Helpful.</b>

Former Member
0 Kudos

Collect : If there are duplicates,COLLECT just adds the similar fields.

If there are no duplicates,it acts as APPEND.

Former Member
0 Kudos

Hi

collect is used for mainly

This statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key. As of Release 6.10, you can use result to set a reference to the inserted or changed row in form of a field symbol or data reference.

Prerequisite for the use of this statement is that wa is compatible with the row type of itab. The row type must be flat and all components that are not part of the table key must have a numeric data type ( i, p, f).

If the internal table does not already contain a row with an identical key, the COLLECT statement has the same effect as the following form of the INSERT statement:

INSERT wa INTO TABLE itab [result].

A row, whose position depends on the table key and the table type, is inserted and filled with the contents of wa.

If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).

The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.

Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.

reward if usefull