Application Development 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: 

How to avoid inner loops

Former Member
0 Kudos
97

Hi abapers,

how can i avoid inner loops in performance point of view.

for example my code as follows .. what are the other statements i can use to get more performance.

if not itab_afvc[] is initial.

loop at itab_afvc.

if not itab_afvc-qmnum is initial.

loop at itab_s where

NLMIS_qmnum eq itab_afvc-qmnum

and opno = itab_afvc-vornr.

if sy-subrc eq 0.

itab_afvc-aufnr1 = itab_s-NLMIS_ORDNO.

itab_afvc-vornr = itab_s-vornr.

modify itab_afvc.

endif.

endloop.

endif.

loop at itab_crhd where objid = itab_afvc-arbid.

if sy-subrc eq 0.

itab_afvc-arbpl = itab_crhd-arbpl.

itab_afvc-werks = itab_crhd-werks.

modify itab_afvc.

endif.

endloop.

if not itab_afko[] is initial.

loop at itab_afko where aufpl = itab_afvc-aufpl.

if sy-subrc eq 0.

itab_afvc-qmnum = itab_afko-qmnum.

modify itab_afvc.

endif.

endloop.

endif.

loop at itab_s where qmnum eq itab_afvc-qmnum

and opno eq itab_afvc-opno.

endloop.

endif.

please help me.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
75

From my understanding you are modifying outer loop internal table so, you can use READ instead of looping the records.

loop at itab_afvc.

if not itab_afvc-qmnum is initial.

Read table itab_s with key

NLMIS_qmnum eq itab_afvc-qmnum

opno = itab_afvc-vornr.

if sy-subrc eq 0.

itab_afvc-aufnr1 = itab_s-NLMIS_ORDNO.

itab_afvc-vornr = itab_s-vornr.

modify itab_afvc.

endif.

endif.

Read table itab_crhd with key objid = itab_afvc-arbid.

if sy-subrc eq 0.

itab_afvc-arbpl = itab_crhd-arbpl.

itab_afvc-werks = itab_crhd-werks.

modify itab_afvc.

endif.

if not itab_afko[] is initial.

read table itab_afko with key aufpl = itab_afvc-aufpl.

if sy-subrc eq 0.

itab_afvc-qmnum = itab_afko-qmnum.

modify itab_afvc.

endif.

endloop.

endif.

endif.

endloop.

May be you can sort all internal tables which are used in READ by the same key and use Binary search.

6 REPLIES 6

Former Member
0 Kudos
75

u can use read table by equating the common key instead of loop and endloop

suresh_datti
Active Contributor
0 Kudos
75

Hi Ramesh,

You can avoid the inner loops by using the

READ TABLE ITAB WITH KEY.. BINARY SEARCH statement.

Regards,

Suresh Datti

Former Member
0 Kudos
75

Hi.

First loop at the internal table. Then read the other table with proper feilds. Try to use Binary search while reading as this improves performance.

Thanks,

Maheshwari.

Former Member
0 Kudos
76

From my understanding you are modifying outer loop internal table so, you can use READ instead of looping the records.

loop at itab_afvc.

if not itab_afvc-qmnum is initial.

Read table itab_s with key

NLMIS_qmnum eq itab_afvc-qmnum

opno = itab_afvc-vornr.

if sy-subrc eq 0.

itab_afvc-aufnr1 = itab_s-NLMIS_ORDNO.

itab_afvc-vornr = itab_s-vornr.

modify itab_afvc.

endif.

endif.

Read table itab_crhd with key objid = itab_afvc-arbid.

if sy-subrc eq 0.

itab_afvc-arbpl = itab_crhd-arbpl.

itab_afvc-werks = itab_crhd-werks.

modify itab_afvc.

endif.

if not itab_afko[] is initial.

read table itab_afko with key aufpl = itab_afvc-aufpl.

if sy-subrc eq 0.

itab_afvc-qmnum = itab_afko-qmnum.

modify itab_afvc.

endif.

endloop.

endif.

endif.

endloop.

May be you can sort all internal tables which are used in READ by the same key and use Binary search.

Former Member
0 Kudos
75

Hi,

Some more additions.

U need not check for initial if U R looping at

something.(if not itab_afko[] is initial).

Also sy-subrc is not required inside a loop.

(if sy-subrc eq 0.)

Also the last loop is not meaningful.

(loop at itab_s where qmnum eq itab_afvc-qmnum

and opno eq itab_afvc-opno.

endloop.) since UR not doing anything here.

Regards,

GSR.

Former Member
0 Kudos
75

Hi

You can't use inner join with LOOP/ENDLOOP statament, you colud improve the performance using SORT TABLE:

DATA: itab_s LIKE SORTED TABLE OF .... with non unique key nlmis_qmnum

opno with header line.

DATA: itab_crhd LIKE SORTED TABLE OF crhd WITH non unique KEY objid

WITH HEADER LINE.

DATA: itab_afko LIKE SORTED TABLE OF afko WITH non unique KEY aufpl

WITH HEADER LINE.

......

LOOP AT itab_afvc.

LOOP AT itab_s WHERE nlmis_qmnum EQ itab_afvc-qmnum

AND opno = itab_afvc-vornr.

itab_afvc-aufnr1 = itab_s-nlmis_ordno.

itab_afvc-vornr = itab_s-vornr.

MODIFY itab_afvc.

ENDLOOP.

LOOP AT itab_crhd WHERE objid = itab_afvc-arbid.

itab_afvc-arbpl = itab_crhd-arbpl.

itab_afvc-werks = itab_crhd-werks.

MODIFY itab_afvc.

ENDLOOP.

LOOP AT itab_afko WHERE aufpl = itab_afvc-aufpl.

itab_afvc-qmnum = itab_afko-qmnum.

MODIFY itab_afvc.

ENDLOOP.

LOOP AT itab_s WHERE qmnum EQ itab_afvc-qmnum

AND opno EQ itab_afvc-opno.

ENDLOOP.

ENDLOOP.

Max