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 loop inside loop.

former_member184624
Participant
0 Kudos

Hi All,

I am BW Consultant and i have written Functio Module extraction using the ABAP Code. Please find the below ABAP Code.

I am using Z table Z175 and BSEG table and moving the data to final internal table.

In Z175, Billing document field is there. It contains more than 1 data for a single billing document and

BSEG table will also have more than 1 data for a single billing document.

Using the Condition Z175-VBELN = BSEG-BELNR i am pulling the data for other fields such as GL Account, Group Account and other details.

I have written the code as loop inside the Loop. Because of this, it is affecting the performance.

Can you please help me on modifying the attached code.

Thanks.

Moderator Message: Search the internet with search terms viz., Parallel cursor for LOOPs, Nested loop optimisation using SORTED TABLEs. Personally i use the latter.

Message was edited by: Suhas Saha

2 REPLIES 2

Kartik2
Contributor
0 Kudos

Hi,

There is no use of us seeing your attached code, because as you have mentioned, you are usilng a custom table and we will not know the fields of the custom table.

Anyways a small advice, it is the developer who should judge for himself if a loop inside loop is really necessary, in many cases it is.

Wherever it is necessary, us it, if you think that is the best solution. One more thing that you can do is to use parallel cursor while using loop inside loop. Parallel cusrsor is a technique of looping through the inner loop as few times as possible.

loop at lt_outer into ls_outer.

     lv_index = sy-tabix.

     loop at lt_inner into ls_inner from lv_index.

     endloop.

endloop.

it can also be achieved  by a where clause.

loop at lt_outer into ls_outer.

     loop at lt_inner into ls_inner

                              where field eq ls_outer-field.

     loop.

loop.

Hope it helps you. Thank you.

Regards,

Kartik

pushkar_dhale
Participant
0 Kudos

Hi Jalina,

Not Active Contributor

Kindly go through following link this will surly help you to improve your performance of nested loop.

http://wiki.sdn.sap.com/wiki/display/Snippets/ABAP+Code+for+Parallel+Cursor+-+Loop+Processing

Regards,

Pushkar.