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.


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.
 
 
 
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com