Tutorial Name

Using Collect and Append in SAP ABAP

Tutorial Description

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

Tutorial Area Core ABAP
Prerequisites ABAP
Learning Level Basic
Estimated Time to learn 15
Save to Learning List Save to your Learning List
+ -
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.

Author | Last Updated| | 7 Comments | F Share

Tutorial Comments

Total Comments: Add your Comment
11 Aug 2015

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

06 Oct 2015

miner difference but useful for me

03 Nov 2015

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 ?

03 Nov 2015

article is very good..simple to learn

29 Nov 2015

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

19 Mar 2016

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

03 Dec 2016

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.

Leave a Comment

Enter Comment Text