[ABAP]collect

Posted tailiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[ABAP]collect相关的知识,希望对你有一定的参考价值。

collect 是将非数字字段作为关键字,对数字字段进行累加求和的工具


REPORT ZCALCUATOR.

TYPES:BEGIN OF TY_TEST,
  ID(3) TYPE C,
  DS(3) TYPE C,
  MENGE TYPE I,
END OF TY_TEST.

DATA:T_TEST TYPE TABLE OF TY_TEST,"定义内表
     W_TEST TYPE TY_TEST."定义工作区
DATA:T_TEST2 TYPE TABLE OF TY_TEST."定义内表


* 对内表追加记录
W_TEST-ID = '001'.
W_TEST-DS = '123'.
W_TEST-MENGE = 10.
APPEND W_TEST TO T_TEST.

W_TEST-ID = '001'.
W_TEST-DS = '123'.
W_TEST-MENGE = 70.
APPEND W_TEST TO T_TEST.

W_TEST-ID = '002'.
W_TEST-DS = '1'.
W_TEST-MENGE = 20.
APPEND W_TEST TO T_TEST.

W_TEST-ID = '002'.
W_TEST-DS = '1'.
W_TEST-MENGE = 50.
APPEND W_TEST TO T_TEST.

W_TEST-ID = '002'.
W_TEST-DS = '3'.
W_TEST-MENGE = 80.
APPEND W_TEST TO T_TEST.

W_TEST-ID = '003'.
W_TEST-DS = '3'.
W_TEST-MENGE = 30.
APPEND W_TEST TO T_TEST.

W_TEST-ID = '003'.
W_TEST-DS = '13'.
W_TEST-MENGE = 90.
APPEND W_TEST TO T_TEST.

* 进行collect 
LOOP AT T_TEST INTO W_TEST.
  COLLECT W_TEST INTO T_TEST2."collect的做法是追加到内表Test2中,但是如果关键字重复,则将数字字段进行累加
  CLEAR:W_TEST.
ENDLOOP.

WRITE:/ 'Collect前的内容:'  .
WRITE:/1(12) '编号' ,   '数量'.
LOOP AT T_TEST INTO W_TEST.
  WRITE:/ W_TEST-ID, ' ',W_TEST-DS ,' ', W_TEST-MENGE.
  CLEAR:W_TEST.
ENDLOOP.

WRITE:/ .


WRITE:/ 'Collect后的结果:'  .
WRITE:/1(12) '编号' ,   '数量'.
LOOP AT T_TEST2 INTO W_TEST.
  WRITE:/ W_TEST-ID, ' ',W_TEST-DS ,' ', W_TEST-MENGE.
  CLEAR:W_TEST.
ENDLOOP.

技术图片

以上是关于[ABAP]collect的主要内容,如果未能解决你的问题,请参考以下文章

SAP ABAP里存在Java List这种集合工具类么?CL_OBJECT_COLLECTION了解一下

[ABAP]collect

ABAP语法-At end of 和collect

ABAP中Collect的用法

ABAP中Collect的用法

金蝶handler中 collection 代码片段理解