Using Collect and Append in SAP ABAP
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.
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.
REPORT ZTEST.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""" COLLECT STATEMENT
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 = 'EMP1'.
WA-SALARY = 5000.
COLLECT WA INTO ITAB. "collect
CLEAR WA.
WA-ID = 2.
WA-NAME = 'EMP2'.
WA-SALARY = 50000.
COLLECT WA INTO ITAB. "collect
CLEAR WA.
WA-ID = 1.
WA-NAME = 'EMP1'.
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.
""""""""""""""""""""""""""""""""""""""" APPEND STATEMENT
REPORT ZTEST.
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.
No comments:
Post a Comment