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

Infinite Loop - Internal Tables

Former Member
0 Likes
2,765

LOOP AT A.

-


---

---

select query - fetch to internal table B.

cnt = 1.

if sy-subrc = 0.

loop at B.

read table B index cnt.

sum = sum + amount

cnt = cnt + 1.

end loop.

The above loop B goes infinite.

I also tried below steps, this also ends in infinite loop but practically this contains only 4 records(for first select)

tablecount of B = C, ( describe )

Do C times.

read table B index sy-index.

sum = sum + amount

enddo.

Another clarification - in the amount filed both - ve and + ve numbers are in the table.

so while i do total will the - ve number get subtracted?.

18 REPLIES 18
Read only

Former Member
0 Likes
2,148

Hi Sona,

use EXIT keyword in the middle of the code so that after you records are completed loop get terminated rather going to infinite.

Cheers!!

VEnk@

Read only

0 Likes
2,148

Yes.Thats the option i have after checking the cnt.Yet to test this option.

But any idea whats wrong in above loop at statemen shown.

Any idea abt the negative total.

Read only

Former Member
0 Likes
2,148

hi,

You are missing one statement in your code.

if sy-subrc = 0 . This statement should come after read statement.

For ex.

cnt = 1.

loop at B.

read table B index cnt.

if sy-subrc = 0.

sum = sum + amount

cnt = cnt + 1.

else.

exit.

endif.

end loop.

Now this will check if there are records in the table then only it will proceed otherwise it will exit.

Similarly, for this one :

Do C times.

read table B index sy-index.

if sy-subrc = 0

sum = sum + amount

else.

exit.

endif.

enddo.

i hope it is clear to you.

Thanx.

Read only

Former Member
0 Likes
2,148

Hi Sona,

First of all, try to avoid giving a select query inside a loop statement.

Give a general select query before the loop. And then inside the loop, give a read statement inside the loop based on the conditions. It will improve processing time tremendously.

As you said, maybe for the first select, there could be only 4 records. But, each during each loop pass n number of records could be selected into B. That could be the reason.

And yes, the negative number will be subtracted.

Cheers,

Vishnu

Read only

Former Member
0 Likes
2,148

what ever code you are showing is the complete code..? i don't think so, show the complete logic..

Read only

Former Member
0 Likes
2,148

Dear Sona,

Since u r using select query inside the loop so there is no need to set cnt value.

just write...

loop at A.

do.

read table B index sy-index.

if sy-subrc ne 0.

exit.

endif.

sum = sum + amount.

enddo.

endloop.

-ve values will get subtracted from amount.

to avoid this use the below code.

if B-amount < 0.

B-amount = B-amount * ( - 1).

endif.

thanks and regards

abhinesh

Read only

0 Likes
2,148

Thanks all.I'm trying sy-subrc after read with exit.

Read only

Former Member
0 Likes
2,148

Hello Sona,

One question from me that whether you ve ended the first loop before going into the second loop aor after the second loop. after that only giving any solutions be possible.

Read only

Former Member
0 Likes
2,148

Hi

Kindly check below comments.

LOOP AT A.

-


---

---

select query - fetch to internal table B.

use describe for the table B. and populate one variable with the count of B Records e.g. variable CNT_B.

cnt = 1.

if sy-subrc = 0.

loop at B.

if cnt gt cnt_b.

exit.

endif.

read table B index cnt.

sum = sum + amount

cnt = cnt + 1.

end loop.

The above loop B goes infinite.

I also tried below steps, this also ends in infinite loop but practically this contains only 4 records(for first select)

tablecount of B = C, ( describe )

Do C times.

read table B index sy-index.

sum = sum + amount

enddo.

I think this will solved you problem.

Regards,

Hiren Patel

Read only

0 Likes
2,148

here is my elaborate code : I tried all means but still infinite loop ..error shown at IF after loop3.before adding this IF error shown at READ statement.

Pls suggest any possibility to find the cause of the infinite loop. any idea to display before pgm gets timedout.

LOOP AT TABLE1.

PERFORM FORM DATA.

ENDLOOP.

FORM DATA.

CNT = 1.

COUNT = 1.

LOOP AT table2.

READ TABLE table2 INDEX CNT.

COUNT = COUNT + 1.

MOVE table2-VAR TO ZZV.

IF TABLE1-value = ZZV.

select * from tabe where field = table2-field into TABLE3.

IF sy-subrc = 0.

describe table TABLE3 LINES table3count.

loop at table3.

if cnt > table3count.

exit.

endif.

read table table3 index cnt.

if sy-subrc = 0.

sum = sum + amount.

cnt = cnt + 1.

else.

exit.

endif.

endloop.

move sum to output.

ELSE.

write : / SY-SUBRC - table3.

endif.

exit.

ENDIF.

endloop. -


END OF loop2.

-


-


-


ENDFORM.

I dont think the reason for infinite loop can be found by debugging.

without loop3 the entire code works fine,I get all other values in the output.

any suggestion to solve.Thanks.

Read only

0 Likes
2,148

The problem is triply nested loops. So have a look at :

[Performance of Nested Loops|]

(They are not infinite loops - just a very large number of loop passes.)

Rob

Edited by: Rob Burbank on Oct 27, 2008 4:31 PM

Read only

0 Likes
2,148

Thanks a lot Rob.I'm checking the possibilities to reduce the run time.

In my case i can't read the internal table 2 with key because i'm comparing this value with some extracted digits from another.these two table dont have foreign key relationships.so i cant use binary search in LOOP 2. ( first loop in the form)..

So i have used counter and IF condition to check the value by redaing each line in internal table 2.

Any suggestions with this? Nice blog.

Read only

0 Likes
2,148

OK - do you have to read each table entry for each loop pass?

Rob

Read only

0 Likes
2,148

I'm not getting your question.

But for the inner most loop ,each time this select gets executed I just need to add all the amounts in the internal table which will be limited no. of records only.

Not sure if there's any way to total only 1 field in internal table without reading it or a loop ...It would be a better option.

Read only

0 Likes
2,148

Hello,

As Rob has suggested, is it really necessary to read you itab again for

each loop pass. You are just reading the itabs sequentially from what I can tell.

Instead of this:

LOOP AT table2.

READ TABLE table2 INDEX CNT.

CNT = CNT + 1.

....

ENDLOOP.

Try this.

LOOP AT table2 into wa_table2.

......

ENDLOOP.

Or instead or a work area you could use a field symbol.

This alone will halve the number of accesses to each itab.

Then you can look at the other suggestions regarding nested loops and

selects within loops to further help with performance.

Regards

Greg Kern

Read only

0 Likes
2,148

I have removed the second loop.

What i did is : Now i 'm assigning the key to a variable and using read with key.

And the pgm works fine for minimum records for a particular selection criteria.

Checking the time for another selection which has large no. of records.I think this is going to stop again.Oh.Got it,but it takes more time .

Not sure what else to change.

Greg, thanks for response.

Thanks Rob.

Edited by: Sona on Oct 27, 2008 11:45 PM

Read only

0 Likes
2,148

Again,The pgm stops at the innermost loop where i'm adding the amts ...when there is large no. of records for the outermost internal table - loop .

Read only

Former Member
0 Likes
2,148

This thread is closed.