首页 > 代码库 > ABAP中Collect的用法

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的用法