ABAP中Collect的用法

Posted HARU_ちゃん

tags:

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

vaule:collect在非数值字段相同的情况下,起到了数值字段汇总作用。

非数值字段不同的情况下,效果和append相同执行插入内表操作,当非数值字段相同的时候,则相当于modify的效果,只不过是将数值字段进行汇总相加后更新。

语法:collect W_table into I_table .

代码如下:


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

DATA:I_TEST TYPE TABLE OF TY_TEST,
     W_TEST TYPE TY_TEST.
DATA:I_TEST2 TYPE TABLE OF TY_TEST.

W_TEST-ID = \'001\'.
W_TEST-MENGE = 10.
APPEND W_TEST TO I_TEST.

W_TEST-ID = \'001\'.
W_TEST-MENGE = 70.
APPEND W_TEST TO I_TEST.

W_TEST-ID = \'002\'.
W_TEST-MENGE = 20.
APPEND W_TEST TO I_TEST.

W_TEST-ID = \'002\'.
W_TEST-MENGE = 50.
APPEND W_TEST TO I_TEST.

W_TEST-ID = \'002\'.
W_TEST-MENGE = 80.
APPEND W_TEST TO I_TEST.

W_TEST-ID = \'003\'.
W_TEST-MENGE = 30.
APPEND W_TEST TO I_TEST.

W_TEST-ID = \'003\'.
W_TEST-MENGE = 90.
APPEND W_TEST TO I_TEST.


LOOP AT I_TEST INTO W_TEST.
  COLLECT W_TEST INTO I_TEST2."关键一步。。。"
  CLEAR:W_TEST.
ENDLOOP.

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


WRITE:/ .


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


以上是关于ABAP中Collect的用法的主要内容,如果未能解决你的问题,请参考以下文章

ABAP中Collect的用法

abap中modify和collect的用法?哪位能给个详细的解释!!谢谢

ABAP语法-At end of 和collect

java8 .stream().sorted().filter().map().collect()用法

[ABAP]collect

新的 ABAP 语法而不是 COLLECT