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 sum total of existing records in ITAB.
- If no, it will append or add a new record.
Example program on collect
REPORT demo_int_tables_COLLECT .
DATA: BEGIN OF line,
col1(3) TYPE c,
col2(2) TYPE n,
col3 TYPE i,
END OF line.
col1(3) TYPE c,
col2(2) TYPE n,
col3 TYPE i,
END OF line.
DATA itab LIKE SORTED TABLE OF line
WITH NON-UNIQUE KEY col1 col2.
WITH NON-UNIQUE KEY col1 col2.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 3.
COLLECT line INTO itab.
WRITE / sy-tabix.
COLLECT line INTO itab.
WRITE / sy-tabix.
line-col1 = 'def'. line-col2 = '34'. line-col3 = 5.
COLLECT line INTO itab.
WRITE / sy-tabix.
COLLECT line INTO itab.
WRITE / sy-tabix.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 7.
COLLECT line INTO itab.
WRITE / sy-tabix.
COLLECT line INTO itab.
WRITE / sy-tabix.
LOOP AT itab INTO line.
WRITE: / line-col1, line-col2, line-col3.
ENDLOOP.
WRITE: / line-col1, line-col2, line-col3.
ENDLOOP.
The list output is:
1
2
1
abc 12 10
def 34 5
The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECTstatement, the first line of itab is modified. The following diagram shows the three steps:
No comments:
Post a Comment