collect 用法

COLLECT allows you to create unique or summarized datasets. The system first tries to find a table entry corresponding to the table key (see Key definition for internal tables ). The key values are taken either from the header line of the internal table itab, or from the explicitly-specified work area wa. itab must have a flat structure, that is, it may not contain other internal tables. All components that are not part of the key must be have numeric types (see ABAP numeric types).
If the system finds an entry, the numeric fields that are not part of the table key (see ABAP number types) are added to the sum total of the existing entries. If it does not find an entry, the system creates a new entry instead.
 
例子1 :
report zcp_testd7 .
data: wa_sflight type sflight,
      it_sflight type table of sflight.
wa_sflight-CARRID = 'AA'.
wa_sflight-PRICE = 1000.
collect wa_sflight into it_sflight.
loop at it_sflight into wa_sflight.
write:/ wa_sflight-CARRID,
        wa_sflight-PRICE.
endloop.

wa_sflight-CARRID = 'AB'.
wa_sflight-PRICE = 1000.
collect wa_sflight into it_sflight.
loop at it_sflight into wa_sflight.
write:/ wa_sflight-CARRID,
        wa_sflight-PRICE.
endloop.
 
 
结果:
test
AA             1,000.00
AA             1,000.00
AB             1,000.00
 
 
例子2
report zcp_testd7 .
data: wa_sflight type sflight,
      it_sflight type table of sflight.
wa_sflight-CARRID = 'AA'.
wa_sflight-PRICE = 1000.
collect wa_sflight into it_sflight.
loop at it_sflight into wa_sflight.
write:/ wa_sflight-CARRID,
        wa_sflight-PRICE.
endloop.

wa_sflight-CARRID = 'AA'.
wa_sflight-PRICE = 1000.
collect wa_sflight into it_sflight.
loop at it_sflight into wa_sflight.
write:/ wa_sflight-CARRID,
        wa_sflight-PRICE.
endloop.
 
结果:
test
AA             1,000.00
AA             2,000.00
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值