‎2007 Jan 23 2:15 PM
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
‎2007 Jan 23 2:19 PM
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.
‎2007 Jan 23 2:18 PM
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
‎2007 Jan 23 2:21 PM
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.
‎2007 Jan 23 2:19 PM
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.
‎2007 Jan 23 2:26 PM
thanks chandra.
what if objnr is blank in t_final ... i will miss that record ?
‎2007 Jan 23 2:36 PM
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.
‎2007 Jan 23 2:26 PM
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 .
‎2007 Jan 23 2:54 PM
Check this:
/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops
Rob