Sunday, November 13, 2016

COLLECT

Syntax: COLLECT wa INTO itab.

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.
Using COLLECT statement in SAP ABAP

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.
DATA itab LIKE SORTED TABLE OF line
 WITH NON-UNIQUE KEY col1 col2.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 3.
COLLECT line INTO itab.
WRITE / sy-tabix.
line-col1 = 'def'. line-col2 = '34'. line-col3 = 5.
COLLECT line INTO itab.
WRITE / sy-tabix.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 7.
COLLECT line INTO itab.
WRITE / sy-tabix.
LOOP AT itab INTO line.
 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:
This graphic is explained in the accompanying text

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...