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: 

Endless Loop

benjamin_allsopp
Active Participant
0 Kudos
221

Hi I'm trying to output a routing report.

It all goes well until I process my internal tables to an alv.The code is getting stuck in a loop and I can't work out why.

Problem code below. Any ideas? Thanks in advance.

loop at gt_routing_hdr into ls_routing_hdr.

       clear ls_display_op.

       move-corresponding ls_routing_hdr to ls_display_op.

       loop at gt_routing_item into ls_routing_item where plnnr = ls_routing_hdr-plnnr.

*                                                     and plnal = ls_routing_hdr-plnal.

         move-corresponding ls_routing_item to ls_display_op.

* Get BOM information ignoring alternative BOMs

         read table gt_mast into ls_mast with key matnr = ls_display_op-matnr.

         if sy-subrc = 0.

           ls_display_op-stlnr = ls_mast-stlnr.

           ls_display_op-bmeng = ls_mast-bmeng.

           ls_display_op-bmein = ls_mast-bmein.

         endif.

* Get Production version data

         read table gt_mkal into ls_mkal with key matnr = ls_display_op-matnr

                                                  werks = ls_display_op-werks

                                                  plnnr = ls_display_op-plnnr

                                                  alnal = ls_display_op-plnal.

         if sy-subrc = 0.

           ls_display_op-stlal = ls_mkal-stlal.

           ls_display_op-verid = ls_mkal-verid.

           ls_display_op-mdv01 = ls_mkal-mdv01.

         endif.

* Get the costing lot size and planned future prices

         read table gt_marc into ls_marc

         with key matnr = ls_display_op-matnr

                  werks = ls_display_op-werks.

         ls_display_op-mtart = ls_marc-mtart.

         ls_display_op-losgr = ls_marc-losgr.

         ls_display_op-zplp1 = ls_marc-zplp1.

         ls_display_op-zplp2 = ls_marc-zplp2.

         ls_display_op-zkprs = ls_marc-zkprs.

         append ls_display_op to gt_display_op.

       endloop.

     endloop.

     sort gt_display_op by matnr plnnr plnal plnkn.

11 REPLIES 11

Former Member
0 Kudos
149

Hi Benjamin,


1. Use Parallel cursor.

2. Before writing LOOP and READ statement SORT the TWO INTERNAL TABLES with KEY and use BINARY SEARCH.

3. Use SY-SUBRC check after LOOP & READ statement.

         

4. Replace the  Statetement as below.   

  loop at gt_routing_item into ls_routing_item where plnnr =ls_display_op-plnnr.


5. Clear the workarea after the Loop.

Thanks & Regards,

Seshadri.

venuarun
Active Participant
0 Kudos
149

Hi Benjamin,

First you loop your item table

   loop at gt_routing_item into ls_routing_item where plnnr = ls_routing_hdr-plnnr.

*                                                     and plnal = ls_routing_hdr-plnal.


Inside this loop use the READ statement to get the header details

Read table git_header into gwa_header with key   (Your condition)



Endloop.




With Regards

Arun VS

archanapawar
Contributor
0 Kudos
149

Hi Benjamin,

You should avoid loop inside loop. In this case, you can easily do so.

Loop at item table, gt_routing_item, first and then, inside the loop you can read gt_routing_hdr table passing plnnr and plnal value from item table.

Former Member
0 Kudos
149

Hi,

Please take care of below points:

1. Loop through the item table.

2. Read the header table followed by other tables as did by you in code.

3. Table which are being READ should be sorted on the KEYS and in each READ statement put a BINARY SEARCH. It increases the performance.

4. Instead of using MOVE CORRESPONDING FIELDS pass the value of each field as:

ls_display_op-field1 = ls_routing_item-field1.

ls_display_op-field2 = ls_routing_item-field2.

Regards,

Aashika

Former Member
0 Kudos
149

Hi Benjamin,

You have to apply parallel cursor method here as you are using nested loop. Try below code and let me know if it works.

DATA: lv_index type sy-tabix.

  SORT gt_routing_hdr by plnnr.

  SORT gt_routing_item by plnnr.

  loop at gt_routing_hdr into ls_routing_hdr.

       clear ls_display_op.

       move-corresponding ls_routing_hdr to ls_display_op.

       clear lv_index.

       read table gt_routing_item into ls_routing_item with key plnnr = ls_routing_hdr-plnnr BINARY SEARCH.

       if sy-subrc eq 0.

         lv_index = sy-tabix.

       endif. 

       loop at gt_routing_item into ls_routing_item from lv_index. "where plnnr = ls_routing_hdr-plnnr.

*                                                     and plnal = ls_routing_hdr-plnal.

         move-corresponding ls_routing_item to ls_display_op.

* Get BOM information ignoring alternative BOMs

         read table gt_mast into ls_mast with key matnr = ls_display_op-matnr.

         if sy-subrc = 0.

           ls_display_op-stlnr = ls_mast-stlnr.

           ls_display_op-bmeng = ls_mast-bmeng.

           ls_display_op-bmein = ls_mast-bmein.

         endif.

* Get Production version data

         read table gt_mkal into ls_mkal with key matnr = ls_display_op-matnr

                                                  werks = ls_display_op-werks

                                                  plnnr = ls_display_op-plnnr

                                                  alnal = ls_display_op-plnal.

         if sy-subrc = 0.

           ls_display_op-stlal = ls_mkal-stlal.

           ls_display_op-verid = ls_mkal-verid.

           ls_display_op-mdv01 = ls_mkal-mdv01.

         endif.

* Get the costing lot size and planned future prices

         read table gt_marc into ls_marc

         with key matnr = ls_display_op-matnr

                  werks = ls_display_op-werks.

         ls_display_op-mtart = ls_marc-mtart.

         ls_display_op-losgr = ls_marc-losgr.

         ls_display_op-zplp1 = ls_marc-zplp1.

         ls_display_op-zplp2 = ls_marc-zplp2.

         ls_display_op-zkprs = ls_marc-zkprs.

         append ls_display_op to gt_display_op.

       endloop.

     endloop.

     sort gt_display_op by matnr plnnr plnal plnkn.

himanshu_gupta13
Product and Topic Expert
Product and Topic Expert
0 Kudos
149

Hi Benjamin,

Kindly do the sorting in both the internal tables before doing looping in ascending order, also use the binary search for read statements, when you use binary search on read statement must sort that internal table, Also, chech sy-subrc eq 0 after read statement.

I think lot of data records are in internal tables, hence use the parallel cursor technique for looping.

Many Thanks / Himanshu Gupta

Former Member
0 Kudos
149

HI Benjamin,

I guess the endless loop is due to the recursive of BOM for materials.You are trying to read the MAST table with a MATNR field which will be getting updated to the looping table. Instead of reading the table MAST repeatedly and appending the same data to the table you can have one buffer table which stores all the materials which are returned and before reading the MAST table table read the buffer table if the material is present in buffer then you can skip the logic of appending the MAST table to the internal table and if buffer table does not have the material then you can append the material to the internal table from MAST table.

Regards,

Chakradhar.

Former Member
0 Kudos
149

Hi Benjamin

loop at gt_routing_hdr into ls_routing_hdr.

       move-corresponding ls_routing_hdr to ls_display_op.

Read Table gt_routing_item into ls_routing_item with key plnnr = ls_routing_hdr-plnnr.

if sy-subrc = 0.

move-corresponding ls_routing_item to ls_display_op

ENDIF.

* Get BOM information ignoring alternative BOMs

         read table gt_mast into ls_mast with key matnr = ls_display_op-matnr.

         if sy-subrc = 0.

           ls_display_op-stlnr = ls_mast-stlnr.

           ls_display_op-bmeng = ls_mast-bmeng.

           ls_display_op-bmein = ls_mast-bmein.

         endif.

* Get Production version data

         read table gt_mkal into ls_mkal with key matnr = ls_display_op-matnr

                                                  werks = ls_display_op-werks

                                                  plnnr = ls_display_op-plnnr

                                                  alnal = ls_display_op-plnal.

         if sy-subrc = 0.

           ls_display_op-stlal = ls_mkal-stlal.

           ls_display_op-verid = ls_mkal-verid.

           ls_display_op-mdv01 = ls_mkal-mdv01.

         endif.

* Get the costing lot size and planned future prices

         read table gt_marc into ls_marc

         with key matnr = ls_display_op-matnr

                  werks = ls_display_op-werks.

         ls_display_op-mtart = ls_marc-mtart.

         ls_display_op-losgr = ls_marc-losgr.

         ls_display_op-zplp1 = ls_marc-zplp1.

         ls_display_op-zplp2 = ls_marc-zplp2.

         ls_display_op-zkprs = ls_marc-zkprs.

         append ls_display_op to gt_display_op.

clear : ls_routing_hdr,ls_routing_item,ls_mast,ls_marc,ls_display_op

     endloop.

     sort gt_display_op by matnr plnnr plnal plnkn.

Thanks

Vamsi

former_member207661
Active Participant
0 Kudos
149

Hi Benjamin,

Based on your code, it is clear that it is not going in endless loop.

It might be taking more time due to huge data.

As suggested by everyone, you need to improve the code performance by:

1) Looping at Item data and then read header data

2) Use of Binary Search in Read. Make sure that a table is sorted in ASCENDING order on same keys as Binary search on that table.

Warm Regards,

Shyam Agrawal

VenkatRamesh_V
Active Contributor
0 Kudos
149

Hi,

Try.

Replace work area by field symbols.

Loop through item table.

Before using Read statement sort the internal by key, add BINARY SEARCH.

Hope it helpful.

Regards,

Venkat.

benjamin_allsopp
Active Participant
0 Kudos
149

Thank you guys.

Looping at the Item table and reading the header as I was doing with the other tables, coupled with binary search has stopped the report from coming to a run time error.

I've tried to mark all the answers that I used as helpful and have awarded the most detailed with correct answer.

I didn't get round to trying the parallel cursor or field symbols as it is now solved so I am unable to comment on how effective these methods are.