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: 

Add fields to existing internal table and collect statement

Former Member
0 Kudos
978

Hi Experts,

I am modifying a report to add some fields in the output (ALV).

The final internal table ITAB is field using collect statement in a loop which has numeric and non-numeric fields.

Suppose there are 2 entries in my internal table

First entry:

RLDNR (c2) :Z4

RYEAR(N4):2010

OBJNR:000000000000002157O

BLART:BLANK (new field added)

Second entry

RLDNR (c2) :Z4

RYEAR(N4):2010

OBJNR:000000000000028453

BLART(C2):GA

Collect itab.

I was expecting that the above 2 rows should be added using collect statement but as i have added BLART it is appending the data in ITAB.

How can i avoid this ?

11 REPLIES 11

TuncayKaraca
Active Contributor
0 Kudos
246

@Bhanu Malik

COLLECT is used to summate numerical entries in an internal table by looking all other non-numeric values. If all non-numeric values are same it sums up numeric values and updates proper record, but if any non-numeric field is not same it appends a new record.

For your example if you COLLECT these two records, you will see two records in the internal table.

0 Kudos
246

Thats correct and since BLART in my above example is blank and GA it is creating 2 entries rather than 1.

I have to add this field in report o/p and so i was thinking by just adding this field to o/p table and not changing much existing logic it should work.

Can workaround be done with collect statement?

0 Kudos
246

Be careful - it could treat the year as a numeric and sum on that field as well.

Rob

0 Kudos
246

I don't think you can workaround by using COLLECT in this case. Basically you need to LOOP the records and build up your own collect statement. Inside LOOP, check if all non-numeric values (except BLART) are already existing, then sum up numerical values and MODIFY internal table, otherwise append a new record.

But the question comes up that: What are you going to do with BLART values in MODIFY case?

0 Kudos
246

By the way COLLECT statement is obsolete!

There is a good information about COLLECT out there:
COLLECT

0 Kudos
246

We just want to show BLART as o/p on alv report the above logic is used at end while preparing the final internal table to display in alv

0 Kudos
246

You may keep existing internal table for COLLECT and use a different internal table with BLART field for ALV.

0 Kudos
246

Thanks Tuncay for your inputs.

0 Kudos
246

Dear Banu,

first write a select query to fetch all the BLART records from DB table into an Internal table say 'X' based on the given conditions.

Ex:select blart from DB table into table x where ......(conditions)

and then loop your final internal table say 'Y' and read corresponding record from table 'X' and modify table Y record.

Ex:loop at Y

read table X into X with key ...(condition).

Modify Y from X.

endloop.

by this way u need not to change your final internal table data you will just add BLART to that table.

Hope this will solve your problem.

ShyamPindiproli
Active Participant
0 Kudos
246

Hello Bhanu,

All of the fields that are not part of the table key must have a numeric type (F, I, or P) are used for computation and all other fields are considered as key fields unless otherwise defined.

COLLECT adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

Either define Table Key for  the internal table or keep all the non-numeric type fields left justified should solve your problem.

   TYPES : BEGIN OF ty_line,
                RLDNR(2) TYPE C,
                RYEAR(4) TYPE N,
                BLART(2) TYPE C,
                OBJNR    TYPE I,
       END OF ty_LINE.

or use the keyword

TYPES ITAB TYPE SORTED TABLE OF LINE WITH UNIQUE KEY COLUMN1 to define the key fields and Collect should work.

Regards,

Shyam 

Former Member
0 Kudos
246

Hi Bhanu,

Collect statement calculates the sub total of non-character fields(P,I,F) by taking character fields as key. In your case all fields those i can see is of only character type. So, it's working similar to the append statement.

Thanks & Regards,

Samir