Application Development and Automation 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: 
Read only

Loop Inside loop, time out error.

Former Member
0 Likes
2,489

Pls look into the below code, In this im looping inside a loop.

Im looping thru the internal table( say 2000 records ) which has data from a select query, inside this loop i need to calculate the total of output qty which is available in the another VIEW/TABLE which might return 10 records in each iteration of the inner loop.

This process takes huge time to process also gives out runtime out error.

pls help.

LOOP AT itablnpln INTO walnpln.

contot = 0.

  • This particular query loop takes large time to process

SELECT distinct * INTO CORRESPONDING FIELDS OF TABLE itablnp

FROM zvprdconf where aufnr = walnpln-aufnr and

j_3asize = walnpln-j_3akordx and stzhl eq ' ' and stokz ne 'X'.

IF sy-subrc EQ 0.

*Earlier i used this looping.

*loop at itablnp into walnp.

  • contot = contot + walnp-J_3ALMNGA.

  • move walnp-J_3ASIZE to walnpln-J_3ASIZE.

  • endloop.

*Currently tried with the following,

LOOP AT itablnp ASSIGNING <fs_itablnp>.

contot = contot + <fs_itablnp>-J_3ALMNGA.

move <fs_itablnp>-J_3ASIZE to walnpln-J_3ASIZE.

ENDLOOP.

walnpln-output = contot.

move zvprdconf-isdd to walnpln-zdate.

move zvprdconf-isdz to walnpln-ztime.

endif.

ENDLOOP.

regs,

Raja

15 REPLIES 15
Read only

Former Member
0 Likes
1,978

Hi Raja,

Use where condition in nested loop. Means the loop you are using inside loop. give where condition.

Ashven

Read only

Former Member
0 Likes
1,978

Hi Raja,

Use where condition in nested loop. Means the loop you are using inside loop. give where condition.

Ashven

Read only

Former Member
0 Likes
1,978

hi

write the below code... iseteasd of selecting inside the loop... first fetch for entries then do the processing..


IF NOT ITABLNPLN IS INITIAL.
  SELECT DISTINCT *
  INTO CORRESPONDING FIELDS OF TABLE ITABLNP
  FROM ZVPRDCONF
   FOR ALL ENTRIES IN ITABLNPLN
  WHERE AUFNR = ITABLNPLN-AUFNR AND
       J_3ASIZE = ITABLNPLN-J_3AKORDX AND
       STZHL EQ ' ' AND
       STOKZ NE 'X'.
  IF SY-SUBRC = 0.
    SORT ITABLNPLN.
  ENDIF.
ENDIF.

LOOP AT ITABLNPLN INTO WALNPLN.

  CONTOT = 0.

  LOOP AT ITABLNP ASSIGNING <FS_ITABLNP> WHERE AUFNR = WALNPLN-AUFNR
                                            AND J_3ASIZE = WALNPLN-J_3AKORDX.
    CONTOT = CONTOT + <FS_ITABLNP>-J_3ALMNGA.
    MOVE <FS_ITABLNP>-J_3ASIZE TO WALNPLN-J_3ASIZE.
  ENDLOOP.
  WALNPLN-OUTPUT = CONTOT.
  MOVE ZVPRDCONF-ISDD TO WALNPLN-ZDATE.
  MOVE ZVPRDCONF-ISDZ TO WALNPLN-ZTIME.
ENDLOOP.

avoide using correspondig fields of table.....c

Please Close this thread.. when u r problem is solved

Reward if Helpful

Regards

Naresh Reddy K

Message was edited by:

Naresh Reddy

Read only

0 Likes
1,978

Dear Naresh,

I tried your suggestions, but still getting the time out runtime error.

Pls see below, Error coming at indicated line of code.

if not itablnpln is initial.

SELECT distinct * INTO CORRESPONDING FIELDS OF TABLE itablnp

FROM zvprdconf FOR ALL ENTRIES IN itablnpln

where aufnr = itablnpln-aufnr and

j_3asize = itablnpln-j_3akordx and stzhl eq ' ' and stokz ne 'X'.

if sy-subrc = 0.

sort itablnpln.

endif.

endif.

148 LOOP AT itablnpln INTO walnpln.

<b><u>>>>>> contot = 0.</u></b> 150 IF sy-subrc EQ 0.

151 LOOP AT itablnp ASSIGNING <fs_itablnp>

152 contot = contot + <fs_itablnp>-J_3AL

153 move <fs_itablnp>-J_3ASIZE to walnpl

154 ENDLOOP.

155

156 walnpln-output = contot.

157 move zvprdconf-isdd to walnpln-zdate.

158 move zvprdconf-isdz to walnpln-ztime.

159 endif.

regs,

Raja

Read only

0 Likes
1,978

hi raja wht uis the type of <b>contot</b>... u r here calculating the value...

check the type.. and check whether the value is overflowing.....

Read only

0 Likes
1,978

Dear Naresh,

The field <u><b>CONTOT</b></u> is of type i ( Integer), i found that is not overflowing. The error is also not because of the overflow value. pls find the error report as below.

<b>ShrtText

Time limit exceeded.

What happened?

The program "SAPMZLPR" has exceeded the maximum permitted runtime without

interruption, and has therefore been terminated.

What can you do?

Print out the error message (using the "Print" function)

and make a note of the actions and input that caused the

error.

To resolve the problem, contact your SAP system administrator.

You can use transaction ST22 (ABAP Dump Analysis) to view and administer

termination messages, especially those beyond their normal deletion

date.

is especially useful if you want to keep a particular message.

Error analysis

After a certain length of time, the program is terminated. In the cas

of a work area, this means that

- endless loops (DO, WHILE, ...),

- database accesses producing an excessively large result set,

- database accesses without a suitable index (full table scan)

do not block the processing for too long.

The system profile "rdisp/max_wprun_time" contains the maximum runtim

program. The

current setting is 600 seconds. Once this time limit has been exceede

the system tries to terminate any SQL statements that are currently

being executed and tells the ABAP processor to terminate the current

program. Then it waits for a maximum of 60 seconds. If the program is

still active, the work process is restarted.

successfully processed, the system gives it another 600 seconds.

Hence the maximum runtime of a program is at least twice the value of

the system profile parameter "rdisp/max_wprun_time".

How to correct the error

You should usually execute long-running programs as batch jobs.

If this is not possible, increase the system profile parameter

"rdisp/max_wprun_time".

Depending on the cause of the error, you may have to take one of the

following measures:

- Endless loop: Correct program;

- Dataset resulting from database access is too large:

Instead of "SELECT * ... ENDSELECT", use "SELECT * INTO internal table

(for example);

- Database has an unsuitable index: Check index generation.

You may able to find an interim solution to the problem

in the SAP note system. If you have access to the note system yourself,

use the following search criteria:

-


"TIME_OUT" C

"SAPMZLPR" or "SAPMZLPR"

"START-OF-SELECTION"

-


If you cannot solve the problem yourself and you wish to send

an error message to SAP, include the following documents:

1. A printout of the problem description (short dump)

To obtain this, select in the current display "System->List->

Save->Local File (unconverted)".

2. A suitable printout of the system log

To obtain this, call the system log through transaction SM21.

Limit the time interval to 10 minutes before and 5 minutes

after the short dump. In the display, then select the function

"System->List->Save->Local File (unconverted)".

3. If the programs are your own programs or modified SAP programs,

supply the source code.</b>

===========================================================

if not itablnpln is initial.

SELECT distinct * INTO CORRESPONDING FIELDS OF TABLE itablnp

FROM zvprdconf FOR ALL ENTRIES IN itablnpln

where aufnr = itablnpln-aufnr and

j_3asize = itablnpln-j_3akordx and stzhl eq ' ' and stokz ne 'X'.

if sy-subrc = 0.

sort itablnpln.

endif.

endif.

LOOP AT itablnpln INTO walnpln.

<b> >>>>>></b> <b><i><u>contot = 0.</u></i></b> IF sy-subrc EQ 0.

LOOP AT itablnp ASSIGNING <fs_itablnp> where aufnr = walnpln-aufnr and j_3asize =

contot = contot + <fs_itablnp>-J_3ALMNGA.

move <fs_itablnp>-J_3ASIZE to walnpln-J_3ASIZE.

ENDLOOP.

walnpln-output = contot.

move zvprdconf-isdd to walnpln-zdate.

move zvprdconf-isdz to walnpln-ztime.

endif.

walnpln-wip = walnpln-menge - contot.

Read only

0 Likes
1,978

HI RAJA,

can u plese send the complete code.

1.

use the followindg code..for select.. use select then delete adjacent duplicates.. avoid distincs..

IF NOT ITABLNPLN IS INITIAL.
  SELECT  *
  INTO CORRESPONDING FIELDS OF TABLE ITABLNP
  FROM ZVPRDCONF
   FOR ALL ENTRIES IN ITABLNPLN
  WHERE AUFNR = ITABLNPLN-AUFNR AND
        J_3ASIZE = ITABLNPLN-J_3AKORDX AND
        STZHL EQ ' ' AND
        STOKZ NE 'X'.
  IF SY-SUBRC = 0.
    SORT ITABLNPLN.
    DELETE ADJACENT DUPLICATE FROM ITABLNPLN COMPARING ALL FIELDS.
  ENDIF.
ENDIF.

Read only

0 Likes
1,978

Dear Naresh,

Even after changing the code as below, i m getting the same error.pls help.

IF SY-SUBRC = 0.

SORT ITABLNPLN.

DELETE ADJACENT DUPLICATE FROM ITABLNPLN COMPARING ALL FIELDS.

ENDIF.

pls find the complete code as below.

code]

&----


*& Report SAPMZLINEPLANRPT

*&

&----


*&

*&

&----


INCLUDE ZLPR_TOP.

*----


*Event AT Selection-Screen on Block Param.

*----


AT SELECTION-SCREEN ON BLOCK sorter.

CHECK All = 'X'.

*----


*Event Start-Of-Selection.

*----


START-OF-SELECTION.

Data: zstat type jest-stat.

data: stklocaf type mska-lgort.

data: stklocas type mska-lgort.

FIELD-SYMBOLS <fs_itablnp> LIKE LINE OF itablnp.

CLEAR it_ZVPRODDET.

SELECT DISTINCT ZVPODbukrs ZVPODaufnr ZVPOD~objnr

jeststat ZVPODwerks ZVPODarbpl ZVPODJ_3AKORD2

zvpodplnbez zvpodaufnr zvpodkdauf zvpodkdpos

zvpodj_3akord1 zvpodj_3akord2 zvpod~j_3akordx

zvpodj_3absnr zvpodmatnr zvpod~j_4kscat

zvpodmvgr4 zvpodj_3acada zvpodj_4krcat zvpodmenge

zvpodposnr zvpodkunnr zvpodaufpl zvpodaplzl

zvpodplnnr zvpodarbid zvpodsteus zvpodbismt

zvpodwerks zvpodj_3akvgr6 zvpod~j_3arqda

zvpodmvgr5 zvpodaudat zvpod~j_3addat

zvpodvtweg zvpodobjnr zvpod~bukrs

zvpodfsavd zvpodssavd zvpod~bstkd

zvpodbstdk zvpodihrez zvpod~bstkd_e

zvpodbstdk_e zvpodihrez_e zvpod~arbpl

zvpodvgw01 zvpodkvgr2 zvpod~vsart

FROM ZVPOD

INNER JOIN jest ON ZVPODobjnr = jestobjnr

INTO CORRESPONDING FIELDS OF table it_ZVPRODDET where

plnbez in FGM and arbpl in wc and werks in plant

and SSAVD in eldate

and J_3AKORD2 in cups and jest~inact ne 'X'.

SORT it_ZVPRODDET BY aufnr stat.

LOOP AT it_ZVPRODDET INTO wa_ZVPRODDET.

IF wa_ZVPRODDET-stat = 'I0045' . " TECO - compl

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr.

ELSEIF wa_ZVPRODDET-stat = 'I0009' . " CNF - Confirmed

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr.

ELSEIF wa_ZVPRODDET-stat = 'I0012' . " DLV - Delivered

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr.

ELSEIF wa_ZVPRODDET-stat = 'I0046' . " CLSD - Closed

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr.

ELSEIF wa_ZVPRODDET-stat = 'I0076' . " DLFL - Del Flag

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr.

ELSEIF wa_ZVPRODDET-stat = 'E0003' . " SCLS - Short Close

CLEAR tj30t.

SELECT SINGLE txt04

FROM tj30t INTO tj30t-txt04

WHERE stsma = 'PRDHOLD' AND

estat = 'E0003' AND

txt04 = 'SCLS' AND

spras = 'EN'.

IF sy-subrc = 0.

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr.

ENDIF.

ENDIF.

  • Condition for Prod Order released - REL

IF wa_ZVPRODDET-stat = 'I0002'.

mreleased = 'Y'.

else.

mreleased = 'N'.

endif.

zstat = wa_ZVPRODDET-stat.

IF mreleased = 'N'.

DELETE it_ZVPRODDET WHERE aufnr = wa_ZVPRODDET-aufnr

and stat = zstat.

ENDIF.

ENDLOOP.

case mark.

when All.

LOOP AT it_ZVPRODDET INTO wa_ZVPRODDET.

zaufnr = wa_zvproddet-aufnr.

zarbid = wa_zvproddet-arbid.

move wa_zvproddet to walnpln.

append walnpln to itablnpln.

endloop.

clear walnpln.

if not itablnpln is initial.

SELECT distinct * INTO CORRESPONDING FIELDS OF TABLE itablnp

FROM zvprdconf FOR ALL ENTRIES IN itablnpln

where aufnr = itablnpln-aufnr and

j_3asize = itablnpln-j_3akordx and stzhl eq ' ' and stokz ne 'X'.

if sy-subrc = 0.

sort itablnpln by aufnr j_3asize.

DELETE ADJACENT DUPLICATES FROM ITABLNPLN COMPARING ALL FIELDS.

endif.

endif.

LOOP AT itablnpln INTO walnpln.

contot = 0.

IF sy-subrc EQ 0.

LOOP AT itablnp ASSIGNING <fs_itablnp> where aufnr = walnpln-aufnr and j_3asize = walnpln-j_3akordx.

contot = contot + <fs_itablnp>-J_3ALMNGA.

move <fs_itablnp>-J_3ASIZE to walnpln-J_3ASIZE.

ENDLOOP.

walnpln-output = contot.

move zvprdconf-isdd to walnpln-zdate.

move zvprdconf-isdz to walnpln-ztime.

endif.

walnpln-wip = walnpln-menge - contot.

if walnpln-werks = '1000'.

stklocaf = '1050'.

stklocas = '1060'.

elseif walnpln-werks = '2000'.

stklocaf = '2150'.

stklocas = '2160'.

endif.

select single kunnr into walnpln-ship from vbpa where

vbeln = walnpln-KDAUF and PARVW = 'WE'.

zship = walnpln-ship.

move zship to walnpln-ship.

select single kalab into walnpln-zactqty from mska

where matnr = walnpln-plnbez

and j_3asize = walnpln-J_3AKORDX and LGORT = stklocaf.

condense walnpln-kdauf.

zsales = walnpln-kdauf.

concatenate zsales 'S' into zso.

select single kalab into walnpln-zsndqty from mska

where matnr = walnpln-plnbez

and j_3asize = walnpln-J_3AKORDX and LGORT = stklocas

and J_4KSCAT = zso.

zmatn = walnpln-plnbez.

zsale = walnpln-KDAUF.

walnpln-kdauf = zsale.

walnpln-plnbez = zmatn.

zcust = walnpln-kunnr.

walnpln-kunnr = zcust.

select single bezei into walnpln-season from TVV2T where

kvgr2 = walnpln-kvgr2 and SPRAS = 'E'.

select single bezei into walnpln-shipmode from T173T where

vsart = walnpln-vsart and SPRAS = 'E'.

STRL = strlen( walnpln-j_3akord2 ).

if strl = 4.

move walnpln-j_3akord2 to walnpln-j_3akord3.

clear walnpln-j_3akord2.

endif.

move zremk to walnpln-remk.

MODIFY itablnpln FROM walnpln.

contot = 0.

clear itablnp.

ENDLOOP.

PERFORM build_fieldcatalog.

PERFORM build_layout.

PERFORM display_alv_report.

endcase.

[/code]

regs,

raja[

Read only

0 Likes
1,978

raja

i have told u to removew the select Distinct..

remove the select <b>distinnct</b> and then sort the table.. remove the adjacent duplicates.

2. i have told u write the select out side the loop..but still it inside the loop.

3.u r removing the records from table it_zvproddet... but i have told u there is an alternative..

4.there are alomost 5-6 selects inside the loop. remove all these .. select the data bofore the loop into internal tables use for all entires.. then use READ statement to get the records

this pogram is completly poor..

<b>plese follow wht i have told..

but u have given me the smae code</b>

Message was edited by:

Naresh Reddy

Read only

0 Likes
1,978

I agree with Naresh, just a small correction

if sy-subrc = 0.

sort itablnpln by aufnr j_3asize.

DELETE ADJACENT DUPLICATES FROM ITABLNPLN COMPARING ALL FIELDS.

endif.

endif.

sort itablnp by aufnr j_3asize. <<< make sure your itablnp is sorted, in this case your inner loop below will be faster....I prefer to use "read table binary search... loop from sy-tabix... if any of key fields changes -> exit the loop".

LOOP AT itablnpln INTO walnpln.

contot = 0.

IF sy-subrc EQ 0. <<<< not sure why would you need this check here...

LOOP AT itablnp ASSIGNING <fs_itablnp> where aufnr = walnpln-aufnr and j_3asize = walnpln-j_3akordx.

contot = contot + <fs_itablnp>-J_3ALMNGA.

move <fs_itablnp>-J_3ASIZE to walnpln-J_3ASIZE.

ENDLOOP.

Read only

Former Member
0 Likes
1,978

This will help you quite a bit:

/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops

Rob

Read only

Former Member
0 Likes
1,978

Hi Raja,

Try using 'SUM' as following,

LOOP AT itablnp ASSIGNING <fs_itablnp>.

contot = contot + <fs_itablnp>-J_3ALMNGA.

move <fs_itablnp>-J_3ASIZE to walnpln-J_3ASIZE.

ENDLOOP.

Instead of this please try using the following,

LOOP AT itablnp.

SUM(contot)

ENDLOOP.

Or try using 'SUM ' aggregate function in the SELECT statement itself.

Hope this will help!

Best Regards

Ali S

Read only

Former Member
0 Likes
1,978

Hi,

Use the parallel processing technique.

SORT ITABLNP BY AUFNR J_3ASIZE.

DATA: LV_TABIX TYPE SYTABIX.

LOOP AT ITABLNPLN INTO WALNPLN.

CONTOT = 0.

<b> READ TABLE ITABLNP TRANSPORTING NO FIELDS

WITH KEY AUFNR = WALNPLN-AUFNR

J_3ASIZE = WALNPLN-J_3AKORDX

BINARY SEARCH.

IF SY-SUBRC = 0.</b>

<b> LV_TABIX = SY-TABIX.</b>

<b> LOOP AT ITABLNP ASSIGNING <FS_ITABLNP> FROM LV_TABIX.

  • EXIT CONDITION

IF <FS_ITABLNP>-AUFNR <> WALNPLN-AUFNR OR

<FS_ITABLNP>-J_3ASIZE <> WALNPLN-J_3AKORDX.

EXIT.</b>

ENDIF.

CONTOT = CONTOT + <FS_ITABLNP>-J_3ALMNGA.

MOVE <FS_ITABLNP>-J_3ASIZE TO WALNPLN-J_3ASIZE.

ENDLOOP.

<b> ENDIF.</b>

WALNPLN-OUTPUT = CONTOT.

MOVE ZVPRDCONF-ISDD TO WALNPLN-ZDATE.

MOVE ZVPRDCONF-ISDZ TO WALNPLN-ZTIME.

ENDLOOP.

Thanks,

Naren

Read only

Former Member
0 Likes
1,978

Change the code as below.

LOOP AT ITABLNPLN INTO WALNPLN.

CONTOT = 0.

READ TABLE ITABLNP WITH KEY AUFNR = WALNPLN-AUFNR

J_3ASIZE = WALNPLN-J_3AKORDX.

IF SY-SUBRC = 0.

LOOP AT ITABLNP FROM SY-TABIX

ASSIGNING <FS_ITABLNP> .

IF <FS_ITABLNP>-AUFNR = WALNPLN-AUFNR AND

<FS_ITABLNP>- J_3ASIZE = WALNPLN-J_3AKORDX.

CONTOT = CONTOT + <FS_ITABLNP>-J_3ALMNGA.

MOVE <FS_ITABLNP>-J_3ASIZE TO WALNPLN-J_3ASIZE.

ELSE

EXIT.

ENDIF

ENDLOOP.

WALNPLN-OUTPUT = CONTOT.

MOVE ZVPRDCONF-ISDD TO WALNPLN-ZDATE.

MOVE ZVPRDCONF-ISDZ TO WALNPLN-ZTIME.

ENDLOOP.

Read only

Former Member
0 Likes
1,978

hi,

use

loop at walnpln.

use select sum(....) into walnpln-... from.....

and then modify that

modify walnpln.

endloop.

so no need to secound loop at all..

i think this will help u....