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

Internal table

Former Member
0 Likes
1,262

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,231

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.

16 REPLIES 16
Read only

Former Member
0 Likes
1,231

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

Read only

0 Likes
1,231

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.

Read only

Former Member
0 Likes
1,232

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.

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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.

Read only

0 Likes
1,231

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.

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

i mean what do u mean by combinantions.

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

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

Read only

0 Likes
1,231

Thnk u very much Anversha . My problem is solved.