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

Problem with collect itab

Former Member
0 Likes
808

What is wrong with my coding?

DATA:

BEGIN OF itab1 OCCURS 0,

matnr type mseg-matnr,

mjahr type mseg-mjahr,

erfmg type mseg-erfmg,

END OF itab1.

select MATNR MJAHR ERFMG

into corresponding fields of table itab1

from mseg

where bwart = '501'.

collect itab1.

sort itab1.

I thought I get a sum per Jahr for each material. When I look into the itab I get more then one entry per year per material.

e.g.:

Mat Year Sum

123 2007 100

123 2007 009

What am I doing wrong, I thought the collect sums it up?

chris

1 ACCEPTED SOLUTION
Read only

ThomasZloch
Active Contributor
0 Likes
780

Hi Chris,

you have to put the "collect" into a loop, as it does not work on the entire table at once.

as in

loop at itab into wa.
  collect wa into itab2.
endloop.

you might also be able to get the desired result directly with the select by removing the "TABLE" addition and adding ENDSELECT.

Cheers

Thomas

5 REPLIES 5
Read only

Former Member
0 Likes
779

SELECT matnr mjahr erfmg
INTO CORRESPONDING FIELDS OF itab1
FROM mseg
WHERE bwart = '501'.
  COLLECT itab1.
ENDSELECT.

Read only

ThomasZloch
Active Contributor
0 Likes
781

Hi Chris,

you have to put the "collect" into a loop, as it does not work on the entire table at once.

as in

loop at itab into wa.
  collect wa into itab2.
endloop.

you might also be able to get the desired result directly with the select by removing the "TABLE" addition and adding ENDSELECT.

Cheers

Thomas

Read only

Former Member
0 Likes
779

HI,

Write the select statement in between select and endselect if you are using collect statement.

Or

Once you get the data into itab, do as below :

Create another internal table and work area same as itab.

loop at itab into wa.

collect wa to itab2.

endloop.

Thanks,

Sriram Ponna.

Read only

0 Likes
779

Just as a side note, you may want to reconsider getting rid of the into corresponding fields and just send this into table Itab. This will help you in performance.

Read only

Former Member
0 Likes
779

stat. collect sums up all curr or quantities fields and it should be with in the loop.

it checks each and every record, if it matches with same record, its sums the curr/quantities to same record.

SELECT matnr mjahr erfmg

INTO CORRESPONDING FIELDS OF itab1

FROM mseg

WHERE bwart = '501'.

COLLECT itab1.

ENDSELECT.