Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

AT NEW in loops

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

former_member181962
Active Contributor
0 Kudos

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

10 REPLIES 10

Former Member
0 Kudos
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

abdul_hakim
Active Contributor
0 Kudos

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

former_member181962
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

0 Kudos

Please use caution using this 'On change of' statement, because it is an obsolete statement as per sap documentation.

0 Kudos

Thanks for letting me know Ravi.

Kind regards

Andrew

Former Member
0 Kudos

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