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

Avoiding Nested Loops ?

Former Member
0 Likes
2,102

Hi,

Pls suggest a way to avoid the nested loops of this kind.

itab1 and itab2 are internal tables with header line.


 loop at itab1.
  loop at itab2 where fld1 = itab1-fld1.
<a few statements here>
  endloop.
 endloop.

Useful help would be rewarded.

8 REPLIES 8
Read only

Former Member
0 Likes
1,057

hi,

you can replace with the read statement, provided you need only one matching record from the internal table.

loop at itab1.

read table itab2 with key fld1 = itab1-fld1.

endloop.

if you have more than one matchin record, then use parallel cursor method. see the sample code [here|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/abap%2bcode%2bfor%2bparallel%2bcursor%2b-%2bloop%2bprocessing]

regards,

madhumitha

Read only

Former Member
0 Likes
1,057

Hi,

If it is a one - one relation with itab1 then use Read statement and do your logic

If its like item header relation

loop item table and read header table.

Regards,

Siva chalasani.

Read only

0 Likes
1,057

Hi Siva and Madhumita,

Thanks a lot for your input, but its not a one-one relationship, so i cant use a READ statement.

Read only

0 Likes
1,057

then use parallel cursor method:(sample code in the below link)

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/abap%2bcode%2bfor%2bparallel%2bcursor%2b...

regards,

madhu

Read only

Former Member
0 Likes
1,057

Try using a single LOOP, with a READ TABLE inside!

Like this:



LOOP AT itab1.

READ TABLE itab2 WITH KEY fld1 = itab-fld1
BINARY SEARCH.

<statements>

ENDLOOP.

Read only

Former Member
0 Likes
1,057

Hi,

loop at itab1.

loop at itab2 where fld1 = itab1-fld1.

<a few statements here>

endloop.

endloop.

Instead of above code try with this code.

read table itab2 .

if itab-fld1 = itab2-fld1.

<statement>

Endif.

Endloop.

Reward if useful.

Regards,

Narasimha

Read only

Former Member
0 Likes
1,057

Hi,

loop at itab1.

loop at itab2 where fld1 = itab1-fld1.

<a few statements here>

endloop.

endloop.

Instead of above code try with this code.

loop at itab1.

read table itab2 .

if itab-fld1 = itab2-fld1.

<statement>

Endif.

Endloop.

Reward if useful.

Regards,

Narasimha

Read only

Former Member
0 Likes
1,057

Hi Madhu...

Thanks a lot for your help.