Difference between append, collect and insert command

Addition of rows can be done either by using append or insert command. Collect command can also be used for the same. So whats the difference between all 3 ? This post will cover it up.

Append command:

Append is used to add entries to end of the internal table. Append will not work for Hashed tables. Append will lead to dump in case you try to add entries in improper sort manner in case of sorted tables.
Ex: append wa_mara TO it_mara.  
"Append lines of" can be used to append another internal table contents having same structure to the other internal table.

Ex: APPEND LINES OF it_mara2 to it_mara.

Insert command:

Insert is used to add entries to internal table in the index(line number) specified. Be careful to use this in sorted table else exception/dump will occur if you don’t specify the exact line number according to the sort.
Ex: INSERT wa_mara INTO it_mara INDEX 3.  
Insert lines of can be used to copy another internal table contents having same structure to the internal table. We need to mention the line number also from where it will be copied. The existing entries will be shifted accordingly.
Ex: INSERT LINES OF it_mara2 INTO it_mara index 1. 
If you don’t want to specify line position explicitly , then you can use insert ... into table.
Ex: INSERT wa_mara INTO TABLE it_mara. 
Similarly :
Ex: INSERT LINES OF it_mara2 INTO TABLE it_mara. 
The line number will be automatically determined based on table type:

  1. Standard tables: The line is appended to the end of the internal table. This has the same effect as the APPEND statement.
  2. Sorted tables : The line is inserted into the table automatically according to the sorting of the table key.
  3. Hashed tables : The table is inserted into the internal hash administration according to the table key.

Collect command:

Collect is used for summation purpose. If there is no record with that key , then it will create a entry

If a entry with same key is already present , then it will add the numeric values of the new record to the existing record without changing the non numeric values..

wa_itab-material = '00001'.
wa_itab-qty = 50.
COLLECT wa_itab into it_itab.

wa_itab-material = '00002'.
wa_itab-qty = 100.
COLLECT wa_itab into it_itab.
Now the contents of IT_ITAB is
material   =>  qty
00001     =>   50
00002     =>   100

wa_itab-material = '00001'.
wa_itab-qty = 100.
COLLECT wa_itab into it_itab.
A entry with key material 00001 is already present having qty as 50. qty 100 will be added to it and the value will become 150

Itab contents after collect is
material   =>  qty
00001     =>   150
00002     =>   100

Sy-subrc will be zero if the insertion/collection/appending operation is performed successfully.