03-23-2012 3:57 PM
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 ?
03-23-2012 4:03 PM
@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.
03-23-2012 4:11 PM
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?
03-23-2012 4:15 PM
Be careful - it could treat the year as a numeric and sum on that field as well.
Rob
03-23-2012 4:17 PM
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?
03-23-2012 4:26 PM
By the way COLLECT statement is obsolete!
There is a good information about COLLECT out there:
COLLECT
03-23-2012 4:28 PM
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
03-23-2012 4:38 PM
You may keep existing internal table for COLLECT and use a different internal table with BLART field for ALV.
03-23-2012 5:17 PM
03-24-2012 5:22 AM
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.
03-23-2012 11:18 PM
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
03-24-2012 5:39 AM
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