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: 

difference between append and collect...with simple examples...

Former Member

difference between append and collect with simple examples

thanks

1 ACCEPTED SOLUTION

Former Member

Hi Balaji,

Here is the Difference:

APPEND: This keyword will append the current contents of the table header or the structure to the end of the table as a new record.. This will not affect the any existing record of the table..

itab

field1 field2

1 SAP ABAP

2 SAP BASIS

Now, itab-field1 = 3.

itab-field2 = 'SAP BW'.

APPEND itab.

New contents of itab.

field1 field2

1 SAP ABAP

2 SAP BASIS

3 SAP BW

Similarily, if the table is without header we will use a structure of same type of the table and append it..

e_struc-field1 = 3.

e_struc-field2 = 'SAP BW'.

APPEND e_struc TO tab.

COLLECT : This keyword is used for summation purposes.. when you collect a record to an internal table, it will find that is the table already have any record with the same key values? If yes, then it will add all the numeric values of the new record to the existing record without changing the non numeric values..

But, if there is no record with this key value, it will create a new record.

Ex.

itab

material qty

itab-material = '00001'.

itab-qty = 50.

COLLECT itab.

itab

material qty

00001 50

itab-material = '00002'.

itab-qty = 100.

COLLECT itab.

itab

material qty

00001 50

00002 100

itab-material = '00001'.

itab-qty = 100.

COLLECT itab.

itab

material qty

00001 150

00002 100

Similarily, for a table without header we use..

COLLECT e_struc INTO itab.

Thanks and Best Regards,

Vikas Bittera.

4 REPLIES 4

Former Member

Hi Balaji,

Here is the Difference:

APPEND: This keyword will append the current contents of the table header or the structure to the end of the table as a new record.. This will not affect the any existing record of the table..

itab

field1 field2

1 SAP ABAP

2 SAP BASIS

Now, itab-field1 = 3.

itab-field2 = 'SAP BW'.

APPEND itab.

New contents of itab.

field1 field2

1 SAP ABAP

2 SAP BASIS

3 SAP BW

Similarily, if the table is without header we will use a structure of same type of the table and append it..

e_struc-field1 = 3.

e_struc-field2 = 'SAP BW'.

APPEND e_struc TO tab.

COLLECT : This keyword is used for summation purposes.. when you collect a record to an internal table, it will find that is the table already have any record with the same key values? If yes, then it will add all the numeric values of the new record to the existing record without changing the non numeric values..

But, if there is no record with this key value, it will create a new record.

Ex.

itab

material qty

itab-material = '00001'.

itab-qty = 50.

COLLECT itab.

itab

material qty

00001 50

itab-material = '00002'.

itab-qty = 100.

COLLECT itab.

itab

material qty

00001 50

00002 100

itab-material = '00001'.

itab-qty = 100.

COLLECT itab.

itab

material qty

00001 150

00002 100

Similarily, for a table without header we use..

COLLECT e_struc INTO itab.

Thanks and Best Regards,

Vikas Bittera.

Former Member

Hello Balaji,

<b>Append</b> -->as the key word suggests is used to append data to a location..the general example is appending data from work area to an internal table

<b>eg:</b>consider you have record with the following

record1 A,B,C,1,2,3

record2 A,B,C,4,5,6

record3 X,Y,Z,1,2,3

record4 X,Y,M,1,2,3

you have an internal table with same structure of the work area...then only you can use "Append" statement as

Append record1 to internal table

"now internal table will contain the record1 inside it as a record

<b>"Collect"</b> is used for adding numeric data provided certain non-numeric fields are the same

1)By using the "collect" you are simply adding the data of an internal table and passing it to another table of the same type..

2)based on the first few fields the numeric data will be added and appended automatically when the non-numerical field changes..

to be clear..pls see the example

<b>eg</b>:consider that we have appended the 4 records mentioned above using the append statement to int_tab(an internal table with header line)

now if you do...

loop at int_tab.

collect int_tab to final_tab. "Collect from source to destination

endloop.

the final output of <b>int_tab</b> will be <b>"source internal table</b>

record1 A,B,C,1,2,3

record2 A,B,C,4,5,6

record3 X,Y,Z,1,2,3

record4 X,Y,M,1,2,3

the final output of final_tab will contain just 3 records and contain:::::

<b>"Destination internal table</b>

record1 A,B,C,5,7,9 ( record1 and record2 numeric values are added since A,B,C are same..and they come as one record)

record2 X,Y,Z,1,2,3

record3 X,Y,M,1,2,3

record 2 aand record3 will not be added since X,Y are same but Z and M are different and hence they will be apended to the the destination table as new records

Hope the example was clear

Reward if helpful

Regards

Byju

0 Kudos

Good Explaination

Former Member
0 Kudos

Please check below example will explain the exact difference between append and collect statements

If you want to enter the value into internal table with out duplicate entries then below two methods (internal table with out header line) second is better in performance.

1) LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-field1 TO gs_itab1-field1.

APPEND gs_itab1 TO gt_itab1.

ENDLOOP.

DELETE ADJACENT DUPLICATES FROM gt_itab1 COMPARING field1.

2) LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-field1 TO gs_itab1-field1.

COLLECT gs_itab1 INTO gt_itab1.

ENDLOOP.

Try this/Code :-

Try this :- For CHAR type

TYPES: BEGIN OF gty_type,

num(3) TYPE c,

END OF gty_type.

DATA: gt_itab TYPE TABLE OF gty_type,

gs_itab TYPE gty_type,

gt_itab1 TYPE TABLE OF gty_type,

gs_itab1 TYPE gty_type,

gt_itab2 TYPE TABLE OF gty_type,

gs_itab2 TYPE gty_type.

gs_itab-num = 'ABC'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'DEF'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'DEF'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'GHI'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'GHI'.

APPEND gs_itab TO gt_itab.

  • Insert values to table gt_itab1

CLEAR: gs_itab, gs_itab1.

LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-num TO gs_itab1-num.

APPEND gs_itab1 TO gt_itab1.

CLEAR: gs_itab, gs_itab1.

ENDLOOP.

  • Delete duplicate values from gt_itab1

DELETE ADJACENT DUPLICATES FROM gt_itab1 COMPARING num.

  • Insert only unique values into table gt_itab2

CLEAR: gs_itab, gs_itab2.

LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-num TO gs_itab2-num.

COLLECT gs_itab2 INTO gt_itab2.

CLEAR: gs_itab, gs_itab2.

ENDLOOP.

WRITE: 'GT_ITAB'.

LOOP AT gt_itab INTO gs_itab.

WRITE: / gs_itab-num.

ENDLOOP.

WRITE: / 'GT_ITAB1'.

LOOP AT gt_itab1 INTO gs_itab1.

WRITE: / gs_itab1-num.

ENDLOOP.

WRITE: / 'GT_ITAB2'.

LOOP AT gt_itab2 INTO gs_itab2.

WRITE: / gs_itab2-num.

ENDLOOP.

Try this :- For NUMERIC type

TYPES: BEGIN OF gty_type,

num TYPE i,

END OF gty_type.

DATA: gt_itab TYPE TABLE OF gty_type,

gs_itab TYPE gty_type,

gt_itab1 TYPE TABLE OF gty_type,

gs_itab1 TYPE gty_type,

gt_itab2 TYPE TABLE OF gty_type,

gs_itab2 TYPE gty_type.

gs_itab-num = 10.

APPEND gs_itab TO gt_itab.

gs_itab-num = 20.

APPEND gs_itab TO gt_itab.

gs_itab-num = 20.

APPEND gs_itab TO gt_itab.

gs_itab-num = 30.

APPEND gs_itab TO gt_itab.

gs_itab-num = 30.

APPEND gs_itab TO gt_itab.

  • Insert values to table gt_itab1

CLEAR: gs_itab, gs_itab1.

LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-num TO gs_itab1-num.

APPEND gs_itab1 TO gt_itab1.

CLEAR: gs_itab, gs_itab1.

ENDLOOP.

  • Delete duplicate values from gt_itab1

DELETE ADJACENT DUPLICATES FROM gt_itab1 COMPARING num.

  • Insert only unique values into table gt_itab2

CLEAR: gs_itab, gs_itab2.

LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-num TO gs_itab2-num.

COLLECT gs_itab2 INTO gt_itab2.

CLEAR: gs_itab, gs_itab2.

ENDLOOP.

WRITE: 'GT_ITAB'.

LOOP AT gt_itab INTO gs_itab.

WRITE: / gs_itab-num.

ENDLOOP.

WRITE: / 'GT_ITAB1'.

LOOP AT gt_itab1 INTO gs_itab1.

WRITE: / gs_itab1-num.

ENDLOOP.

Note :- If you try COLLECT with numeric values you will get the sum of that values in single field So collect statement will work differently depends on Type of variable.

Reward points if you find it useful....

Regards,

Minal