Application Development and Automation 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: 
Read only

Loop Within Loop Alternative

Former Member
0 Likes
4,659

what are the other alternative of avoiding loop withing loop , as it will increase lots of processing and load on system.

I know these options :

1. for all entries in

2. Joins

but it is also not much fruitful as it is having some disadvantages.

1 ACCEPTED SOLUTION
Read only

Former Member
3,521

Hi as you suggested you can use FOR ALL ENTRIES and JOINS but in case you are not able to check out the Parallel Cursor Method which avoids the nested loop.

Check out these codes :

Conventional Method


loop at lt_vbpa into wa_vbpa.
  loop at lt_kna1 into wa_kna1 where kunnr = wa_vbpa-kunnr.

****** Your Actual logic within inner loop ******

  endloop.
endloop.

Parallel Cursor Method


sort: lt_vbpa by kunnr,  "Sorting by key is very important
      lt_kna1 by kunnr.  "Same key which is used for where condition is used here
loop at lt_vbpa into wa_vbpa.
  read lt_kna1 into wa_kna1     " This sets the sy-tabix
       with key kunnr = wa_vbpa-kunnr
       binary search.
  if sy-subrc = 0.              "Does not enter the inner loop
    v_kna1_index = sy-tabix.
    loop at lt_kna1 into wa_kna1 from v_kna1_index. "Avoiding Where clause
      if wa_kna1-kunnr <> wa_vbpa-kunnr.  "This checks whether to exit out of loop
        exit.
      endif.

****** Your Actual logic within inner loop ******

   endloop. "KNA1 Loop
  endif.
endloop.  " VBPA Loop

Reward if helpful.

Regards.

8 REPLIES 8
Read only

Former Member
0 Likes
3,521

use parallel cursor method....

Read only

Former Member
0 Likes
3,521

HI,

Use loop from sy-tabix

Read only

Former Member
0 Likes
3,521

hiiii

you can use parallel cursor method to avoid it..use it like below.

clear it_s.
clear it_e.
DATA: incd TYPE i, incs TYPE i,inci TYPE i, incf TYPE i.
DATA: it_tmp TYPE ty_t_data WITH HEADER LINE.
it_tmp[] = it_prd[].
DATA: inc TYPE i.
LOOP AT it_prd.

  READ TABLE it_chs WITH KEY
                            spras  w_langu
                             msehi = it_prd-msehi.

    w_tmp = it_prd-msehi.
  IF sy-subrc NE 0.
    WRITE:/ w_tmp.
    APPEND it_prd TO it_s.
    incs = incs + 1.
  ENDIF.
  if sy-subrc EQ 0.
    append it_prd to it_e.
    endif.
  ENDLOOP.

reward if useful

thx

twinkal

Read only

Former Member
0 Likes
3,521

Hi,

In your previous thread, it was suggested Join can be used.

try that, if not use FOR ALL ENTRIES.

if both are not possible, then use parallel cursor method, as suggested by everyone.

regards,

madhu

Read only

Former Member
0 Likes
3,521

Hi,

First loop at 1st itab and then read 2nd itab.Both the internal tables should have one common key field.

e.g.

Loop at itab.

read table itab2 with key k1 = itab1-key.

-


-


endloop.

Read only

Former Member
0 Likes
3,521

Hi,

Please find the link below which has a tutorial on using the Parallel Cursor which can be a very good alternative to Loop within Loop.

http://www.saptechnical.com/Tutorials/ABAP/ParallelCursor.htm

Reward if helpful.

Warm Regards,

R Adarsh

Read only

Former Member
3,522

Hi as you suggested you can use FOR ALL ENTRIES and JOINS but in case you are not able to check out the Parallel Cursor Method which avoids the nested loop.

Check out these codes :

Conventional Method


loop at lt_vbpa into wa_vbpa.
  loop at lt_kna1 into wa_kna1 where kunnr = wa_vbpa-kunnr.

****** Your Actual logic within inner loop ******

  endloop.
endloop.

Parallel Cursor Method


sort: lt_vbpa by kunnr,  "Sorting by key is very important
      lt_kna1 by kunnr.  "Same key which is used for where condition is used here
loop at lt_vbpa into wa_vbpa.
  read lt_kna1 into wa_kna1     " This sets the sy-tabix
       with key kunnr = wa_vbpa-kunnr
       binary search.
  if sy-subrc = 0.              "Does not enter the inner loop
    v_kna1_index = sy-tabix.
    loop at lt_kna1 into wa_kna1 from v_kna1_index. "Avoiding Where clause
      if wa_kna1-kunnr <> wa_vbpa-kunnr.  "This checks whether to exit out of loop
        exit.
      endif.

****** Your Actual logic within inner loop ******

   endloop. "KNA1 Loop
  endif.
endloop.  " VBPA Loop

Reward if helpful.

Regards.