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.
wa_sflight-CARRID = 'AB'.
wa_sflight-PRICE = 1000.
collect wa_sflight into it_sflight.
wa_sflight-CARRID = 'AA'.
wa_sflight-PRICE = 1000.
collect wa_sflight into it_sflight.
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.
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.
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.
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.
write:/ wa_sflight-CARRID,
wa_sflight-PRICE.
endloop.
结果:
test
AA 1,000.00
AA 1,000.00
AB 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.
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.
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.
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.
write:/ wa_sflight-CARRID,
wa_sflight-PRICE.
endloop.
结果:
test
AA 1,000.00
AA 2,000.00
AA 2,000.00