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 statement

Former Member
0 Kudos
84

hey guys,

Can we use collect statement in same internal table?

loop at itab_list.

collect itab_list.

endloop.

is it correct?

ambichan.

1 ACCEPTED SOLUTION

andreas_mann3
Active Contributor
0 Kudos
51

> hey guys,

> Can we use collect statement in same internal table?

> loop at itab_list.

> collect itab_list.

> endloop.

> is it correct?

-> no.

try this

DATA itab_collect LIKE itab_list OCCURS 0 WITH HEADER LINE.

loop at itab_list.

collect itab_list into itab_collect.

endloop.

Andreas

3 REPLIES 3

ssimsekler
Active Contributor
0 Kudos
51

Hi Ambi

Here is an official explanation:

<i>"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."</i>

By the way, do not forget that, all of the non-key fields of your internal table must have a numeric type (F, I, or P).

*--Serdar <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]</a>

andreas_mann3
Active Contributor
0 Kudos
52

> hey guys,

> Can we use collect statement in same internal table?

> loop at itab_list.

> collect itab_list.

> endloop.

> is it correct?

-> no.

try this

DATA itab_collect LIKE itab_list OCCURS 0 WITH HEADER LINE.

loop at itab_list.

collect itab_list into itab_collect.

endloop.

Andreas

nitschket
Advisor
Advisor
0 Kudos
51

Yes, you can do this... the question rather is: what do you want to achieve?

Suppose your internal (standard) table contains two entries with the same key where the non-key part is just an integer with value 1 in the first line and value 2 in the second line.

After running your little piece of code on this internal table the non-key part of the first line will be 4 and the second line will stay as is.

By the way: COLLECT should rather be used to fill 'empty' tables for performance reasons.

Tom