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: 

how to read the same table twice inside the loop?

Former Member
0 Kudos

hi

i hav loop over ekko.and for 1 record in ekko there are 2 records in mseg i.e 2 movement types bwart....

how to read those two records in the loop ao that i can fill two different fields based on the field bwart...

can anyone please help me

6 REPLIES 6

Former Member
0 Kudos

Hi,

LOOP AT ekko.

read table it_mseg with key mblnr = ekko-ebeln

bwart = '101'.

read table it_mseg with key mblnr = ekko-ebeln

bwart = '102'.

endloop.

Hope it helps!!

Regards,

Pavan

Former Member
0 Kudos

You will have to loop inside loop upto 2 rows and your specified condition....

Or

You can use parallel cursor technique as follows

l_tabix type sy-tabix.
  sort itab1 by <key field>
  sort itab2 by <key field>
  loop at itab1.
    loop at itab2 index l_tabix.
      if (your condition).
      l_tabix = sy-tabix.
        * your code here for the conditon.
      else.
      exit.
    endif.
  endloop.
endloop.

The concept of parallel cursor is to reduce the execution time of the second loop.

This is achieved by starting the second loop from the index for which the last condition is satisfied.

The prerequisite for this is that the tables should be sorted in same order.

Also the condition should be set proper in the loop so that all data is populated.

Regards,

Lalit Mohan Gupta.

Former Member
0 Kudos

Hi,

Loop at itab_ekko.

READ table itab_mseg with key BWART = x.

if sy-subrc eq 0.

statements to modify the itab_mseg

Endif.

READ table itab_mseg with key BWART = y.

if sy-subrc eq 0.

statements to modify the itab_mseg

Endif.

Endloop.

using loop inside a loop will cause performance issue at times, so better use Read statement

Edited by: jeevitha krishnaraj on Apr 7, 2009 2:16 PM

Former Member
0 Kudos

Hi,

You can also add a where clause to your second loop, so an if statement is not needed.

Loop at itab intow wa where field1 = ' 1'.

  • do stuff

Endloop.

It's better to use read statements though this a faster way of working.

Best regards,

Guido Koopmann

Edited by: G. Koopmann on Apr 7, 2009 2:18 PM

Former Member
0 Kudos

Hi dear,

you can use loop into loop with specific condion.

e.g.

Loop at ekko into wa_ekko.

loop at mseg into wa_mseg where mblnr = wa_ekko-ebeln.

if wa_mseg-bwart = '101'.

move table.

elseif wa_mseg-bwart = '102'

move table.

endloop.

append internal table.

clear : wa_ekko, wa_mseg.

endloop.

Hope this is usefull for you.

Regards,

Vijay

0 Kudos

hi,

i hav used the code as below

LOOP AT it_ekKo ASSIGNING <fs_ekKo>.

READ TABLE it_mseg ASSIGNING <fs_mseg>

WITH KEY bwart = c_101

matnr = <fs_ekko>-matnr

werks = <fs_ekko>-werks

ebeln = <fs_ekko>-ebeln

ebelp = <fs_ekko>-ebelp

BINARY SEARCH.

IF sy-subrc EQ 0.

wa_final-migo_qty = <fs_mseg>-menge.

ENDIF.

READ TABLE it_mseg ASSIGNING <fs_mseg>

WITH KEY bwart = c_101

matnr = <fs_ekko>-matnr

werks = <fs_ekko>-werks

ebeln = <fs_ekko>-ebeln

ebelp = <fs_ekko>-ebelp

BINARY SEARCH.

IF sy-subrc EQ 0.

wa_final-isu_qty = <fs_mseg>-menge.

ENDIF.

but the first read it is doing whereas the control is not going to 2nd read.........