‎2007 Aug 14 1:29 PM
‎2007 Aug 14 1:30 PM
Hi..
COLLECT is used to Summarize the Data in internal table while adding the rows.
Collect <wa> into <itab>.
This statement compares the Non-numeric(Type C,N,D,T,X,String) fields of the work area with the Existing rows in the internal table. that means all the Non-numeric fields will act as key (For Eg Matno, Plant)
If a row is found with the same key:
It will add the Numeric fields instead of creating a new row.
If a row is not found with the same key:
It will create a new row like Append.
DATA : BEGIN OF ITAB1 OCCURS 0,
MATNR TYPE MARD-MATNR,
WERKS TYPE MARD-WERKS,
LABST TYPE MARD-LABST,
END OF ITAB.
DATA :WA LIKE ITAB1.
DATA: ITAB2 LIKE ITAB1 OCCURS 0.
SELECT MATNR WERKS LABST FROM MARD INTO TABLE ITAB1.
LOOP AT ITAB1 INTO WA.
COLLECT WA INTO ITAB2.
ENDLOOP.
Check the contents of both ITAB1 AND ITAB2.
<b>Reward if Helpful.</b>
‎2007 Aug 14 1:31 PM
Collect will add the numeric values .
Example
field1 field2
1 10
1 10
collect will give 1 20
‎2007 Aug 14 1:32 PM
This statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key. As of Release 6.10, you can use result to set a reference to the inserted or changed row in form of a field symbol or data reference.
Prerequisite for the use of this statement is that wa is compatible with the row type of itab. The row type must be flat and all components that are not part of the table key must have a numeric data type ( i, p, f).
If the internal table does not already contain a row with an identical key, the COLLECT statement has the same effect as the following form of the INSERT statement:
INSERT wa INTO TABLE itab [result].
A row, whose position depends on the table key and the table type, is inserted and filled with the contents of wa.
If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).
The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.
Following is the test program.
DATA: BEGIN OF seats,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
seatsocc TYPE sflight-seatsocc,
END OF seats.
DATA seats_tab LIKE HASHED TABLE OF seats
WITH UNIQUE KEY carrid connid.
SELECT carrid connid seatsocc
FROM sflight
INTO seats.
COLLECT seats INTO seats_tab.
ENDSELECT.
Reward Points if helpfull.
‎2007 Aug 14 1:41 PM
Hi Mayuri.
when u use collect statement it checks non numeric values in the work area to internal table if it matches then it will add numeric fields and modify the data in internal table.
itab1
abc 10
bcd 20
cde 30
work area
bcd 10
if u use collet statement
collect wa into itab1
then itab1 will be
abc 10
bcd 30
cde 30
reward points to all helpful answers
kiran.M