05-08-2006 12:24 PM
Hi everyone
I have an internal sorted table (itab1) which contains data from table VBAP based on a unique key using VBELN and POSNR. I want to extract the first row for each VBELN into a seperate hashed table (itab2) with a key of VBELN. Two restriction apply:
1. I need to retain the data in POSNR as itab2 will be used to read another internal table later on with a key of VBELN and POSNR
2. I need to keep the contents of itab1 for use later in the program so cannot just do a DELETE ADJACENT...ON VBELN to remove the 'duplicates'.
I have tried the following:
LOOP AT itab1 INTO wa_itab1.
AT NEW vbeln.
INSERT wa_itab1 INTO TABLE itab2.
ENDAT.
ENDLOOP.
However, immediately after the AT NEW command has executed the contents of wa_itab1-posnr become '*****'. My question is therefore, why does it do this and is there a way that I can get the work area to retain the value in POSNR? Perhaps my logic is flawed - is there a better way of achieving my goal?
Thanks
Andrew
05-08-2006 12:28 PM
Do this way,
LOOP AT itab1 INTO wa_itab1.
AT NEW vbeln.
<b>read table itab1 index sy-tabix.</b>
INSERT wa_itab1 INTO TABLE itab2.
ENDAT.
ENDLOOP.
Regards,
Ravi
05-08-2006 12:26 PM
when using AT NEw u have to use read statement
LOOP AT itab1 INTO wa_itab1.
<b>v-inex = sy-tabix</b>
AT NEW vbeln.
<b>read table itab1 into wa_itab1 index v_index.</b>
INSERT wa_itab1 INTO TABLE itab2.
ENDAT.
ENDLOOP.
Message was edited by: Sekhar
05-08-2006 12:27 PM
hi wright,
between at and endat the content of workarea doesn't contain any values.
you can use
on change of wa_itab1-vbeln.
INSERT wa_itab1 INTO TABLE itab2.
endon.
to achieve ur result..
Cheers,
Abdul
05-08-2006 12:28 PM
Do this way,
LOOP AT itab1 INTO wa_itab1.
AT NEW vbeln.
<b>read table itab1 index sy-tabix.</b>
INSERT wa_itab1 INTO TABLE itab2.
ENDAT.
ENDLOOP.
Regards,
Ravi
05-08-2006 12:33 PM
you can avoid the read statement within loop by:
data : wa_itab2 like wa_itab1.
LOOP AT itab1 INTO wa_itab1.
wa_itab2 = wa_itab1.
AT NEW vbeln.
INSERT wa_itab2 INTO TABLE itab2.
ENDAT.
ENDLOOP.
05-08-2006 12:34 PM
Hi,
Declare another work area, wa_itab1_temp like wa_itab1.
And use the following code...
LOOP AT itab1 INTO wa_itab1.
wa_itab1_temp = wa_itab1.
AT NEW vbeln.
INSERT wa_itab1_temp INTO TABLE itab2.
ENDAT.
ENDLOOP.
Thanks and Regards,
Bharat Kumar Reddy.v
05-08-2006 12:35 PM
It happens with all the Control break statements. There is no flaw with your code.
Just rectify the code in the given manner and things will be fine.
LOOP AT itab1 INTO wa_itab1.
AT NEW vbeln.
<b> read table itab1 into wa_itab1 index sy-tabix.</b>
INSERT wa_itab1 INTO TABLE itab2.
ENDAT.
ENDLOOP.
05-08-2006 12:47 PM
Thank you everyone for your replies.
I have opted for the ON CHANGE OF vbeln solutions as it appears to work and seems more elegant and efficient than having to copy data and/or perform additional table reads - unless anybody knows otherwise?
Kind regards
Andrew
05-08-2006 12:53 PM
Please use caution using this 'On change of' statement, because it is an obsolete statement as per sap documentation.
05-08-2006 2:01 PM
05-08-2006 2:39 PM
Hi Andrew,
Best option is to copy the work area data into some other variable and insert as shown below...
LOOP AT itab1 INTO wa_itab1.
move : wa_itab1 to wa_itab2.
AT NEW vbeln.
INSERT wa_itab2 INTO TABLE itab2.
ENDAT. ENDLOOP.
Cheers,
Nilesh