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 on internal table

Former Member
0 Likes
736

Hi ,

I have two internal tables. T_FINAL and T_ITAB.

for each record in T_FINAL there are more than one record in T_ITAB.

OBJNR is the common field.

I have to duplicate the entries in T_FINAL table wrt corresponding entries in T_ITAB based on OBJNR.

Tables are pretty big. How do i go about doing this with optimized results.

Thanks,

amit

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
704

we have to use nested loops only i guess for this

loop at t_final.
 loop at t_itab where objnr eq t_final-objnr.
  move-corresponding t_itab to t_final.
  append t_final.
  clear t_final.
 endloop.
endloop.

7 REPLIES 7
Read only

Former Member
0 Likes
704

Hi,

I hope following code will solve your problem.

LOOP AT t_itab.

READ TABLE t_final WITH KEY objnr = t_itab-objnr.

IF sy-subrc = 0.

...

....

ENDIF.

ENDLOOP.

Reward points if the answer is helpful.

Regards,

Mukul

Read only

0 Likes
704

This is taken fro SE30 hints and tips.

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.

It is the best way to run a nested loop. If you are going to use the read statement, also use the BINARY SEARCH command otherwise it will run slow, especially as you mentioned that there are many entries in the table.

Read only

Former Member
0 Likes
705

we have to use nested loops only i guess for this

loop at t_final.
 loop at t_itab where objnr eq t_final-objnr.
  move-corresponding t_itab to t_final.
  append t_final.
  clear t_final.
 endloop.
endloop.

Read only

0 Likes
704

thanks chandra.

what if objnr is blank in t_final ... i will miss that record ?

Read only

0 Likes
704

If you use this code: -

loop at t_final.

loop at t_itab where objnr eq t_final-objnr.

move-corresponding t_itab to t_final.

append t_final.

clear t_final.

endloop.

endloop.

It will run very slowly. Beleive me I know, I had the same problem. For every record in t_final, it will start reading every record in t_itab. So if you have 10000 records in t_final, it will read all of the records in t_itab 10000 times. If there are 10000 records in t_itab, the number of records read will be 10000 * 10000.

If u use the method I have mentioned, you will not have this problem. The index is stored when reading the table t_itab. Therefore the same record is never read more than once.

If you do not use this method, then do not put another posting on here moaning about the performance of the nested loop.

Read only

Former Member
0 Likes
704

loop at t_itab .

loop at t_final where objnr = t_itab-objnr .

.

.

endloop.

endloop .

If you can change you logic while filling internal table some different logic can also be applied which would be more efficiant .

Read only

Former Member
0 Likes
704

Check this:

/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops

Rob