Application Development and Automation 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: 
Read only

Collect

Former Member
0 Likes
781

Hi,

Whats the use of collect statement?

THanks,

King.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
752

Hi King,

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.

Regards,

Azhar

5 REPLIES 5
Read only

Former Member
0 Likes
753

Hi King,

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.

Regards,

Azhar

Read only

Former Member
0 Likes
752

hi,

if all non-numberic keys of a two records are same then collect will add the numberic key values of two records and makes it as a single record.

regards,

pavan

Read only

former_member404244
Active Contributor
0 Likes
752

Hi,

The following special 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, p). You specify the line wathat you want to add as a work area that is compatible with the line type of itab.

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.

REPORT demo_int_tables_COLLECT .

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 list 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 COLLECTstatement, the first line of itab is modified.

Regards,

Nagaraj

Read only

Former Member
0 Likes
752

Hi,

Collect statement is basically used to sum up all the non-character fields(technically). It is particularly useful when we need to add multiple rows for same primary key.

Regs,

Saurabh

Read only

Former Member
0 Likes
752

if we append rows to an ITAB , the row will be appended at the end of teh ITAB.

if we collect the rows of an ITAB, the numeric fields get added, if the generic key (all non numeric fields) matches with the existing row.

Narendra