‎2006 Nov 26 5:37 AM
hi frnds i hav a doubt in Collect statement .
As per my program its like that
data : begin of itab occurs 0,
f1 type c,
f2 type i,
end of itab.
itab-f1 = 'a'.
itab-f2 = 10.
append itab.
itab-f1 = 'b'.
itab-f2 = 20.
append itab.
itab-f1 = 'c'.
itab-f2 = 30.
append itab.
loop at itab.
write : / itab-f1,
itab-f2.
endloop.
ok frnds when i m writing collect after appending the last record the o/p is like this
a 10
b 20
c 60
why?
can anyone solve my doubt i m debbuging the program but not getting exactly.
regards,
karthik
‎2006 Nov 26 5:46 AM
hi,
If u want to use collect statement ( with header line)
you should use the Clear statement for each and every append.
then you will get correct output.
See the below code.
data : begin of itab occurs 0,
f1 type c,
f2 type i,
end of itab.
itab-f1 = 'a'.
itab-f2 = 10.
append itab.
clear itab.
itab-f1 = 'b'.
itab-f2 = 20.
append itab.
clear itab.
itab-f1 = 'c'.
itab-f2 = 30.
append itab.
clear itab.
collect itab.
loop at itab.
write : / itab-f1,
itab-f2.
endloop.
‎2006 Nov 26 5:42 AM
Karthik,
You have appended three rows in the tables and you getting those three rows in the output, which is right. What else were you expecting?
1. You have not used COLLECT statement.
2. COLLECT will come into affect only when rest of the non-numeric fields are same and you have used the COLLECT statement, so the numeric fields will get added.
Regards,
Ravi
Note - Please mark all the helpful answers
‎2006 Nov 26 5:49 AM
Hi Ravi thks u for ur reply.
see here i hav used collect stmt but my o/p is a 10,b 20,c 60 , here all the non-numeric keys are different but still the numeric values are getting added.
Can u plzz clarify exactly my doubt.
regards,
karthik
data : begin of itab occurs 0,
f1 type c,
f2 type i,
end of itab.
itab-f1 = 'a'.
itab-f2 = 10.
append itab.
itab-f1 = 'b'.
itab-f2 = 20.
append itab.
itab-f1 = 'c'.
itab-f2 = 30.
append itab.
collect itab.
loop at itab.
write : / itab-f1,
itab-f2.
endloop.
‎2006 Nov 26 5:46 AM
hi,
If u want to use collect statement ( with header line)
you should use the Clear statement for each and every append.
then you will get correct output.
See the below code.
data : begin of itab occurs 0,
f1 type c,
f2 type i,
end of itab.
itab-f1 = 'a'.
itab-f2 = 10.
append itab.
clear itab.
itab-f1 = 'b'.
itab-f2 = 20.
append itab.
clear itab.
itab-f1 = 'c'.
itab-f2 = 30.
append itab.
clear itab.
collect itab.
loop at itab.
write : / itab-f1,
itab-f2.
endloop.
‎2006 Nov 26 6:06 AM
ur correct i hav worked out fine. But if at all the non-numreic fields are different why the collect statement is adding up the numeric fields if we dont use clear.
can u plzz clear my doubt.
regrads,
karthik
‎2006 Nov 26 6:12 AM
here you are using internal table with header line . so the collect statement is taking values from previous records.
use the following code you will get exactly what you want
data : begin of itab occurs 0,
f1 type c,
f2 type i,
end of itab.
itab-f1 = 'a'.
itab-f2 = 10.
collect itab.
clear itab.
itab-f1 = 'b'.
itab-f2 = 20.
collect itab.
clear itab.
itab-f1 = 'c'.
itab-f2 = 30.
collect itab.
clear itab.
loop at itab.
write : / itab-f1,
itab-f2.
endloop.
‎2006 Nov 26 6:20 AM
Hi Sekhra,
Sorry but i m not getting the point can u give me clear exp of coolect stmt and make me know exactly how it is working
i got from ur second similar to fst one can u tell me what is the diff.
plzz clear my doubt.
‎2006 Nov 26 7:03 AM
hi,
chk this sample ..u will get a good idea.
DATA: BEGIN OF COMPANIES OCCURS 10,
NAME(20),
SALES TYPE I,
END OF COMPANIES.
COMPANIES-NAME = 'Duck'.
COMPANIES-SALES = 10.
COLLECT COMPANIES.
COMPANIES-NAME = 'Tiger'.
COMPANIES-SALES = 20.
COLLECT COMPANIES.
COMPANIES-NAME = 'Duck'.
COMPANIES-SALES = 30.
COLLECT COMPANIES.The table COMPANIES now has the following appearance:
Duck 40
Tiger 20
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.
rgds
Anver
If you use other statements to insert table entries, you may end up with duplicate entries.
GO THROUGH THIS LINK
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm
‎2006 Nov 26 7:16 AM
Hi Anversha ,
I Hv got an idea from ur exp but as we know that there is no primary key type we use in internal table so how come we say that we are collecting using key.
can u just make me understand this point frnd.
regards,
karthik
‎2006 Nov 26 7:24 AM
hi,
here key means--> only combinations....
Consider only the non-numeric values.
the collect statement will consider all this non-numeric fields as key.
their combination is checked and numeric fields are summed.
Rgds
Anver
‎2006 Nov 26 7:40 AM
Hi Karthik,
Did ur issue solve, pls let me know.
so that i can help.
if solved, kindly close the thread.
Rgds
Anver
<b><i>pls mark all helpful</i></b> asnwers
‎2006 Nov 26 9:11 AM
it means that if we dont clear the last record appended and use collect stmt then it will add up the numeric records , am i correct anevesh.
regrads,
karthik
‎2006 Nov 26 9:13 AM
‎2006 Nov 26 9:14 AM
Hi karthik.
<b><i>ya if the header is not cleared, we will not get the correct ouput.</i></b>
An important TIP for u.
<b>Always use CLEAR statement after an APPEND statement.</b>
otherwise it will create problems.
rgds
Anver
‎2006 Nov 26 9:23 AM
hi anversha,
Thnks for ur replies sory i was in lunch..
can u tell me what does it mean from the material u hav provided it has been written.
if the default key of an internal table is processed with collect is blank , all values are added up in the first table line.
hey can u make my doubt clear.
regards,
karthik
‎2006 Nov 26 9:33 AM
hi,
good.
it means, see this example.
loop at itab.
at end of field1.
collect itab into itab2.
append itab2.
clear itab2.
endat.
endloop.here we are transfering collected data from itab to itab2.
if the itab2 contains a same combination of itab header after collect statemnet, the corresponding line in itab2 is <b>MODIFED</b>,
if no combination is available, the collected record is <b>APPENDED</b>.
hope u understud.
Rgds
Anver
‎2006 Nov 26 9:55 AM