2014 Mar 09 4:27 PM
Hi Experts,
My Scenario is like below. How can I avoid loop inside the loop.
I have to derive my final internal table from my first internal table (Say IT_VBRK2).
My internal table IT_VBRK2 looks like below.
VBELN PSTYV POSNR FKIMG LFIMG
30001 TAN 10 13 0
30001 YB99 900001 13 4
30001 YB99 900002 13 4
30001 YB99 900003 13 5
30002 TAN 10 1 0
30002 TAN 20 1 0
30002 TAN 30 1 0
30002 YB99 900001 1 1
30002 YB99 900002 1 1
30002 YB99 900003 1 1
I need to manipulate my Final internal table from this and the logic is that,
> The rows with TAN values have to be header lines and the corresponding YB99 have to be item values.
> The logic for deciding the line items for a particular TAN is that...
Consider VBELN - 30001----- Here the FKIMG of TAN is 13. So it has to be compared with the YB99 of the same VBELN. So for the first line of YB99 it is 4. which is less than 13. 13 > 4. Next line 13 > 4 + 4 . Next line 13 = 4 + 4+ 5. So the loop has to end there. So our final internal table has to have values like.
30001 TAN 10 13 0
30001 YB99 900001 13 4
30001 YB99 900002 13 4
30001 YB99 900003 13 5 (Same as that of IT_VBRK2)
Now consider VBELN = 30002. and first line.
30002 TAN 10 1 0. So we have to take the first corresponding YB99 value of the same VBELN.
30002 YB99 900001 1 1.
So here the TAN line FKIMG is 1 and YB99 line LFIMG = 1 ...So matching ..
So iterate to next TAN line and next corresponding YB99 line.
30002 TAN 20 1 0
30002 YB99 900001 1 1 TAN-FKIMG = YB99-LFIMG...So iterate to next line.
Like wise I need my final internal table like below.
30001 TAN 10 13 0
30001 YB99 900001 13 4
30001 YB99 900002 13 4
30001 YB99 900003 13 5
30002 TAN 10 1 0
30002 YB99 900001 1 1
30002 TAN 20 1 0
30002 YB99 900002 1 1
30002 TAN 30 1 0
30002 YB99 900003 1 1
I got the output. But I had to use Loop inside loop and it is quite time consuming. (Even for one record it takes so much of time.) Can anyone please guide me to simpler solution. Here I am also attaching the psedo code. (The LOOP statements).
2014 Mar 10 3:35 AM
Hi,
I notice that your final internal table and IT_VBRK2 have the same structure. In other words, you just need a "re-ordering" of your IT_VBRK2. Please try the following pseudo codes.
The complexity of the above loop is O(n), the complexity of sorting is O(n log n), and the complexity of "push" and "pop" is O(1). The overall complexity is O(n log n).
As a final note, if your goal is to avoid nested loop, do NOT use "read table ... with key ..." inside your loop. "Reading a standard table with key inside a loop" is actually a nested loop.
B.R.
2014 Mar 09 6:16 PM
2014 Mar 09 7:12 PM
2014 Mar 09 7:26 PM
Let me explain you what happened. In my previous discussion as you have mentioned. How to avoid loop inside loop?I tried to save what ever I have written. Then it got posted automatically. Then I tried to edit the same , but the result was an error page. I couldnt complete the question and the option to edit also disappeared by this time. Then the only option left before me was to close that thread by assuming the answer and start a new one. I think you got my point. Please do not take anything negative.
BR.
2014 Mar 09 8:27 PM
It's ok. people just often get frustrated when not gettin' any feedback.
to your problem - once you would like to increase the performance and get rid of inner loops, use the inner join in your select.
But from what you wrote, it seems that the logic you need is not implementable using a simple select (or OpenSQL). If your main problem is performance, do these steps first:
1. get rid of work-areas, use field-symbols instead
2. think of use of your internal tables and imagine if you can use sorted table instead of standard table, in read statements use binary search on sorted tables. You will find huge speed increase.
3++ ... if you want to optimize the code, you will find many articles even here, just search. If you need a concrete hint, try to narrow your question to a simpler one, it is much easier to answer then.
2014 Mar 10 2:23 AM
Not really understand the logic, what I can help is just optimize your code to boost up the performance:
SORT IT_TVKBT2 BY VKBUR.
SORT IT_TSPAT2 BY SPART.
SORT IT_KNA12 BY KUNNR.
LOOP AT IT_VBRK2 INTO WA_VBRK2.
IF WA_VBRK2-VGPOS = WA_VBRK2-LPOSNR AND
WA_VBRK2-VGBEL = WA_VBRK2-LVBELN AND
WA_VBRK2-MATNR = WA_VBRK2-LMATNR AND
WA_VBRK2-FKIMG = WA_VBRK2-LFIMG. "Manual Batch
WA_FINALDET-FKDAT = WA_VBRK2-FKDAT.
WA_FINALDET-SPART = WA_VBRK2-SPART.
WA_FINALDET-NETWR = WA_VBRK2-NETWR.
WA_FINALDET-KUNAG = WA_VBRK2-KUNAG.
WA_FINALDET-KUNRG = WA_VBRK2-KUNRG.
WA_FINALDET-SFAKN = WA_VBRK2-SFAKN.
WA_FINALDET-VBELN = WA_VBRK2-VBELN.
WA_FINALDET-LVBELN = WA_VBRK2-LVBELN.
* wa_finaldet-erdat = wa_vbrk2-erdat.
WA_FINALDET-VKBUR = WA_VBRK2-VKBUR.
WA_FINALDET-POSNR = WA_VBRK2-POSNR.
WA_FINALDET-CHARG = WA_VBRK2-CHARG.
WA_FINALDET-MATNR = WA_VBRK2-MATNR.
WA_FINALDET-ARKTX = WA_VBRK2-ARKTX.
WA_FINALDET-FKIMG = WA_VBRK2-FKIMG.
WA_FINALDET-VRKME = WA_VBRK2-VRKME.
WA_FINALDET-ORDER_NUM = WA_VBRK2-POSNR.
READ TABLE IT_TVKBT2 INTO WA_TVKBT2 WITH KEY VKBUR = WA_VBRK2-VKBUR BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-BEZEI = WA_TVKBT2-BEZEI.
ENDIF.
READ TABLE IT_TSPAT2 INTO WA_TSPAT2 WITH KEY SPART = WA_VBRK2-SPART BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-VTEXT = WA_TSPAT2-VTEXT.
ENDIF.
READ TABLE IT_KNA12 INTO WA_KNA12 WITH KEY KUNNR = WA_VBRK2-KUNAG BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-NAME1 = WA_KNA12-NAME1.
WA_FINALDET-NAME2 = WA_KNA12-NAME1.
ENDIF.
APPEND WA_FINALDET TO IT_FINALDET.
CLEAR WA_FINALDET.
CONTINUE.
ENDIF.
IF WA_VBRK2-VGPOS = WA_VBRK2-LPOSNR AND
WA_VBRK2-VGBEL = WA_VBRK2-LVBELN AND
WA_VBRK2-MATNR = WA_VBRK2-LMATNR AND " Automatic Batch First I am taking the all the TAN values to the final internal table.
WA_VBRK2-PSTYV = 'TAN'.
WA_FINALDET-FKDAT = WA_VBRK2-FKDAT.
WA_FINALDET-SPART = WA_VBRK2-SPART.
WA_FINALDET-NETWR = WA_VBRK2-NETWR.
WA_FINALDET-KUNAG = WA_VBRK2-KUNAG.
WA_FINALDET-KUNRG = WA_VBRK2-KUNRG.
WA_FINALDET-SFAKN = WA_VBRK2-SFAKN.
WA_FINALDET-VBELN = WA_VBRK2-VBELN.
WA_FINALDET-SORT_KEY = ''.
WA_FINALDET-LVBELN = WA_VBRK2-LVBELN.
* wa_finaldet-erdat = wa_vbrk2-erdat.
WA_FINALDET-VKBUR = WA_VBRK2-VKBUR.
WA_FINALDET-POSNR = WA_VBRK2-POSNR.
WA_FINALDET-CHARG = WA_VBRK2-CHARG.
WA_FINALDET-MATNR = WA_VBRK2-MATNR.
WA_FINALDET-ARKTX = WA_VBRK2-ARKTX.
WA_FINALDET-FKIMG = WA_VBRK2-FKIMG.
WA_FINALDET-VRKME = WA_VBRK2-VRKME.
WA_FINALDET-ORDER_NUM = WA_VBRK2-POSNR.
READ TABLE IT_TVKBT2 INTO WA_TVKBT2 WITH KEY VKBUR = WA_VBRK2-VKBUR BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-BEZEI = WA_TVKBT2-BEZEI.
ENDIF.
READ TABLE IT_TSPAT2 INTO WA_TSPAT2 WITH KEY SPART = WA_VBRK2-SPART BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-VTEXT = WA_TSPAT2-VTEXT.
ENDIF.
READ TABLE IT_KNA12 INTO WA_KNA12 WITH KEY KUNNR = WA_VBRK2-KUNAG BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-NAME1 = WA_KNA12-NAME1.
WA_FINALDET-NAME2 = WA_KNA12-NAME1.
ENDIF.
* TEMP_VGPOS = WA_VBRK2-VGPOS.
* TEMP_VGBEL = WA_VBRK2-VGBEL.
APPEND WA_FINALDET TO IT_FINALDET.
CLEAR WA_FINALDET.
ENDIF.
ENDLOOP.
DATA: TEMP_LFIMG TYPE LFIMG,
TEMP_FKIMG TYPE FKIMG,
TEMP_FINDET TYPE STANDARD TABLE OF TY_FINALDET,
WA_TEMP_FINDET LIKE LINE OF TEMP_FINDET,
COUNT TYPE I.
SORT IT_VBRK2 BY VBELN MATNR PSTYV LFIMG PROC_KEY.
LOOP AT IT_FINALDET INTO WA_FINALDET WHERE PROC_KEY NE 'X'.
TEMP_FKIMG = WA_FINALDET-FKIMG.
READ TABLE IT_VBRK2 TRANSPORTING NO FIELDS WITH TABLE KEY VBELN = WA_FINALDET-VBELN MATNR = WA_FINALDET-MATNR PSTYV = 'YB99' BINARY SEARCH.
IF SY-SUBRC EQ 0.
LOOP AT IT_VBRK2 INTO WA_VBRK2 FROM SY-TABIX.
"(LOOP INSIDE LOOP ...THIS IS WHAT I WANT TO AVOID)
IF WA_VBRK2-LFIMG EQ WA_FINALDET-FKIMG.
CONTINUE.
ENDIF.
IF WA_VBRK2-VBELN NE WA_FINALDET-VBELN OR WA_VBRK2-MATNR NE WA_FINALDET-MATNR OR WA_VBRK2-PSTYV NE 'YB99'.
EXIT.
ENDIF.
TEMP_LFIMG = TEMP_LFIMG + WA_VBRK2-LFIMG.
IF TEMP_LFIMG LE TEMP_FKIMG.
WA_FINALDET-FKDAT = WA_VBRK2-FKDAT.
WA_FINALDET-SPART = WA_VBRK2-SPART.
WA_FINALDET-NETWR = WA_VBRK2-NETWR.
WA_FINALDET-KUNAG = WA_VBRK2-KUNAG.
WA_FINALDET-KUNRG = WA_VBRK2-KUNRG.
WA_FINALDET-SFAKN = WA_VBRK2-SFAKN.
WA_FINALDET-VBELN = WA_VBRK2-VBELN.
WA_FINALDET-SORT_KEY = 'A'.
WA_FINALDET-LVBELN = WA_VBRK2-LVBELN.
* wa_finaldet-erdat = wa_vbrk2-erdat.
WA_FINALDET-VKBUR = WA_VBRK2-VKBUR.
WA_FINALDET-POSNR = WA_VBRK2-POSNR.
WA_FINALDET-CHARG = WA_VBRK2-CHARG.
WA_FINALDET-MATNR = WA_VBRK2-MATNR.
WA_FINALDET-ARKTX = WA_VBRK2-ARKTX.
WA_FINALDET-FKIMG = WA_VBRK2-FKIMG.
WA_FINALDET-VRKME = WA_VBRK2-VRKME.
WA_FINALDET-ORDER_NUM = WA_VBRK2-LPOSNR.
WA_FINALDET-PROC_KEY = 'X'.
READ TABLE IT_TVKBT2 INTO WA_TVKBT2 WITH KEY VKBUR = WA_VBRK2-VKBUR BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-BEZEI = WA_TVKBT2-BEZEI.
ENDIF.
READ TABLE IT_TSPAT2 INTO WA_TSPAT2 WITH KEY SPART = WA_VBRK2-SPART BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-VTEXT = WA_TSPAT2-VTEXT.
ENDIF.
READ TABLE IT_KNA12 INTO WA_KNA12 WITH KEY KUNNR = WA_VBRK2-KUNAG BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-NAME1 = WA_KNA12-NAME1.
WA_FINALDET-NAME2 = WA_KNA12-NAME1.
ENDIF.
APPEND WA_FINALDET TO IT_FINALDET.
CLEAR WA_FINALDET.
ENDIF.
ENDLOOP.
ENDIF.
READ TABLE IT_VBRK2 INTO WA_VBRK2 WITH KEY VBELN = WA_FINALDET-VBELN MATNR = WA_FINALDET-MATNR
PSTYV = 'YB99' LFIMG = WA_FINALDET-FKIMG PROC_KEY = '' BINARY SEARCH.
IF SY-SUBRC = 0 .
WA_FINALDET-FKDAT = WA_VBRK2-FKDAT.
WA_FINALDET-SPART = WA_VBRK2-SPART.
WA_FINALDET-NETWR = WA_VBRK2-NETWR.
WA_FINALDET-KUNAG = WA_VBRK2-KUNAG.
WA_FINALDET-KUNRG = WA_VBRK2-KUNRG.
WA_FINALDET-SFAKN = WA_VBRK2-SFAKN.
WA_FINALDET-VBELN = WA_VBRK2-VBELN.
WA_FINALDET-SORT_KEY = 'A'.
WA_FINALDET-LVBELN = WA_VBRK2-LVBELN.
* wa_finaldet-erdat = wa_vbrk2-erdat.
WA_FINALDET-VKBUR = WA_VBRK2-VKBUR.
WA_FINALDET-POSNR = WA_VBRK2-POSNR.
WA_FINALDET-CHARG = WA_VBRK2-CHARG.
WA_FINALDET-MATNR = WA_VBRK2-MATNR.
WA_FINALDET-ARKTX = WA_VBRK2-ARKTX.
WA_FINALDET-FKIMG = WA_VBRK2-FKIMG.
WA_FINALDET-VRKME = WA_VBRK2-VRKME.
WA_FINALDET-PROC_KEY = 'X'.
* WA_FINALDET-ORDER_NUM =
READ TABLE IT_TVKBT2 INTO WA_TVKBT2 WITH KEY VKBUR = WA_VBRK2-VKBUR BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-BEZEI = WA_TVKBT2-BEZEI.
ENDIF.
READ TABLE IT_TSPAT2 INTO WA_TSPAT2 WITH KEY SPART = WA_VBRK2-SPART BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-VTEXT = WA_TSPAT2-VTEXT.
ENDIF.
READ TABLE IT_KNA12 INTO WA_KNA12 WITH KEY KUNNR = WA_VBRK2-KUNAG BINARY SEARCH.
IF SY-SUBRC = 0.
WA_FINALDET-NAME1 = WA_KNA12-NAME1.
WA_FINALDET-NAME2 = WA_KNA12-NAME1.
ENDIF.
* WA_TEMP_FINDET-ORDER_NUM = SY-TABIX.
APPEND WA_FINALDET TO IT_FINALDET.
* CLEAR WA_TEMP_FINDET.
WA_VBRK2-PROC_KEY = 'X'.
MODIFY IT_VBRK2 FROM WA_VBRK2 INDEX SY-TABIX TRANSPORTING PROC_KEY.
ENDIF.
CLEAR: TEMP_LFIMG, TEMP_FKIMG.
ENDLOOP.
2014 Mar 10 3:43 AM
Thanks for your reply. I know it is a bit difficult to understand. Let me try to make it simpler. Suppose I have to loop through an internal table to get the final internal table.
Suppose this particular line of data in my first internal table.
30001 TAN 10 13 0. -> Corresponds to many lines of data, for that i have to use loop inside loop. So no read statement possible.
Consider another line of data.
30002 TAN 10 1 1 -> This particlar line of data only corresponds to one line of data. So
I can use read statement.
Is there any way of avoiding loop inside loop?
BR.
2014 Mar 10 8:27 AM
you always can change a loop statement with something like
do.
read....
if (sy-subrc <> 0 )
exit.
endif.
enddo.
But this would not be ok in your case. Please, use sorted tables (not sorted like sort abcd by xyz, but data: lt_table type sorted table...
This will improve your performance a lot.
2014 Mar 11 3:57 PM
2014 Mar 10 3:35 AM
Hi,
I notice that your final internal table and IT_VBRK2 have the same structure. In other words, you just need a "re-ordering" of your IT_VBRK2. Please try the following pseudo codes.
The complexity of the above loop is O(n), the complexity of sorting is O(n log n), and the complexity of "push" and "pop" is O(1). The overall complexity is O(n log n).
As a final note, if your goal is to avoid nested loop, do NOT use "read table ... with key ..." inside your loop. "Reading a standard table with key inside a loop" is actually a nested loop.
B.R.
2014 Mar 11 2:31 PM
Hi,
Post the select & statements that you generate the final internal table. There should be some other linking between the header & line items. Please explain why such a mathematical relation between them?
Thanks,
Shajahan.