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 a loop

Former Member
0 Likes
1,352

is there anything wrong with this code

clear wa_bseg.

loop at t_final into wa_final.

loop at t_bseg1 into wa_bseg where belnr = wa_final-belnr

and gjahr = wa_final-gjahr.

*and zfbdt = wa_final-zfbdt.

if sy-subrc eq 0 .

wa_final1 = wa_final.

append wa_final1 to t_final1.

clear wa_final1.

endif.

clear wa_bseg.

endloop.

*clear wa_final.

endloop.

12 REPLIES 12
Read only

Former Member
0 Likes
1,242

check this..

no need of if sy-subrc in side loop

clear wa_bseg.
loop at t_final into wa_final.
loop at t_bseg1 into wa_bseg where belnr = wa_final-belnr
and gjahr = wa_final-gjahr.

   wa_final1 = wa_final.
   append wa_final1 to t_final1.
   clear wa_final1.

   clear wa_bseg.
endloop.
 clear wa_final.
endloop.

Read only

Former Member
0 Likes
1,242

Hi ,

There is nothing wrong with the code but your code inside the second loop will get executed for no. of rows in outer internal table multiplied by no. of rows in inner internal table .

If you want to restrict means want to execute the inner loop code only once for each row .

Use exit in the inner loop .

Read only

Former Member
0 Likes
1,242

hi

i think there is no problem with this code , just check weather sy-subrc value getting updated properly , depending based on internal loop.

but if data in both internal tables will be more then it's execution time will increase significantly.

Read only

Former Member
0 Likes
1,242

hi,

if you have duplicate entries of belnr and gjahr in the internal table t_final, then there is a possibilty of appending the same data into t_final1. otherwise the code is perfectly fine. as Vijay rightly said, sy-subrc is not needed in loop. if u doign a read then it is required.

Read only

Former Member
0 Likes
1,242

Hi,

I dont know your requirement, but after looking at the code, it looks like you want to find all the wa_final for which there is any entry in t_bseg1 table.

First of all the check of sy-subrc is not required.

If there exists 2 records in t_bseg1 which satisfies the above condition, the wa_final would be appended twice. This can be avoided by using "EXIT" stmt inside the inner loop.

Hope this helps you.

~ Ramanath.

Read only

Former Member
0 Likes
1,242

hi

i think there is no problem with this code , just check weather sy-subrc value getting updated properly , depending based on internal loop.

but if data in both internal tables will be more then it's execution time will increase significantly.

Read only

Former Member
0 Likes
1,242

U need to sort for the fields inside the

loop where ur putting where inserting further more for program performance .

Read only

Former Member
0 Likes
1,242

Hi,

Instead of using loop within loop , use read statement within loop it will improve program performance.

try this below logic..

clear wa_bseg.

loop at t_final into wa_final.

read table t_bseg1 into wa_bseg with key belnr = wa_final-belnr

gjahr = wa_final-gjahr.

if sy-subrc eq 0 .

wa_final1 = wa_final.

append wa_final1 to t_final1.

endif.

clear: wa_bseg, wa_final1,wa_final.

endloop.

Regards,

N M Poojari.

Edited by: Nilambari Poojari on May 27, 2008 9:06 AM

Read only

0 Likes
1,242

A READ on a standard table won't significantly improve performance without the BINARY SEARCH addition.

Rob

Read only

0 Likes
1,242

You need to use the parallel cursor technique. SE30 has hints and tips. The code you are looking for is this.

I = 1.

LOOP AT ITAB1 INTO WA1.

LOOP AT ITAB2 INTO WA2 FROM I.

IF WA2-K <> WA1-K.

I = SY-TABIX.

EXIT.

ENDIF.

" ...

ENDLOOP.

ENDLOOP.

Read only

Former Member
0 Likes
1,242

Hi,

1. I think there is one thing wrong according to me.

2. you are looping at the itab t_final

and inside the loop itself,

you are appending to the same internal table.

3. Instead you should take another internal table,

and move the field values

and append the records.

regards,

amit m.

Read only

Former Member
0 Likes
1,242

Hi,

There is nothing wrong in your code, but rather using nested loop you can do as below :


loop at t_final into wa_final.
read t_bseg1 with key where belnr = wa_final-belnr
                                          gjahr = wa_final-gjahr.

if sy-subrc eq 0 .
  wa_final1 = wa_final.
  append wa_final1 to t_final1.
  clear : wa_final,
            wa_final1.
endif.
endloop.

Thanks,

Sriram Ponna.