Using Collect and Append in SAP ABAP

Author | Last Updated| | 12 Comments Facebook Share Twitter Share Google Plus Share A+ A-


Using collect and append statements in SAP ABAP, difference between append and collect

COLLECT is a statement which is similar to APPEND, the differences are :
  • The COLLECT statement checks weather the work area record already exists with same key field value (the key field must be of type C, N, D, T).
  • If yes, it will add the numerical fields to the existing record.
  • If no, it will append or add a new record.

Using COLLECT statement in SAP ABAP

Example program on collect

REPORT ZSAPN_SUM_COLLECT.
TYPES: BEGIN OF TY_DATA, "user defined type
       ID TYPE N ,
       NAME TYPE CHAR20,
       SALARY TYPE I,
      END OF TY_DATA.
DATA : ITAB TYPE TABLE OF TY_DATA. "internal table
DATA : WA TYPE TY_DATA. "work area

WA-ID = 1.
WA-NAME = 'Sapnuts'.
WA-SALARY = 5000.
COLLECT WA INTO ITAB. "collect
CLEAR WA.

WA-ID = 2.
WA-NAME = 'SAPabap'.
WA-SALARY = 50000.
COLLECT WA INTO ITAB. "collect
CLEAR WA.

WA-ID = 1.
WA-NAME = 'Sapnuts'.
WA-SALARY = 15000.
COLLECT WA INTO ITAB. "collect
CLEAR WA.

LOOP AT ITAB INTO WA.
  WRITE:/ WA-ID, WA-NAME, WA-SALARY. "loop and display data
ENDLOOP.
Using Append in SAP ABAP
When ever we use append statement, it will add a record at the bottom of the internal table.

Example program using APPEND in SAP ABAP

REPORT ZSAPN_SUM_APPEND.
TYPES: BEGIN OF TY_DATA, "user defined type
       ID TYPE N ,
       NAME TYPE CHAR20,
       SALARY TYPE I,
      END OF TY_DATA.
DATA : ITAB TYPE TABLE OF TY_DATA. "internal table
DATA : WA TYPE TY_DATA. "work area

WA-ID = 1.
WA-NAME = 'Sapnuts'.
WA-SALARY = 5000.
APPEND WA TO ITAB. "APPEND
CLEAR WA.

WA-ID = 2.
WA-NAME = 'SAPabap'.
WA-SALARY = 50000.
APPEND WA TO ITAB. "APPEND
CLEAR WA.

WA-ID = 1.
WA-NAME = 'Sapnuts'.
WA-SALARY = 15000.
APPEND WA TO ITAB. "APPEND
CLEAR WA.

LOOP AT ITAB INTO WA.
  WRITE:/ WA-ID, WA-NAME, WA-SALARY. "loop and display data
ENDLOOP.

Tutorial Comments

Total Comments: Add your Comment
29 Mar 2018

Thanks........Good Work.........

29 Mar 2018

miner difference but useful for me

29 Mar 2018

Hi I just found the difference between the programs is the statements used, then when we use the Collect statement the here what particular key has been used ?

29 Mar 2018

article is very good..simple to learn

29 Mar 2018

statement is good. But, it's not collecting all the data values....!

29 Mar 2018

YUP. This article is very good for beginner. simple to learn. good work. carry on.

29 Mar 2018

This article is really good. I would like to say that if you add even the snapshot of the how the output will be. It will be more helpful.

29 Mar 2018

Very Helpful and easy to understand!! Thanks!!

29 Mar 2018

article is very good..simple to learn

29 Mar 2018

dingiri program no usefulxxxxxxxxxxxx

26 Sep 2018

Ghanta useful...!!! Show difference with output then it will b more clear.

Leave a Comment

Enter Comment Text