‎2008 Oct 27 4:04 AM
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?.
‎2008 Oct 27 4:14 AM
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@
‎2008 Oct 27 4:23 AM
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.
‎2008 Oct 27 4:24 AM
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.
‎2008 Oct 27 4:28 AM
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
‎2008 Oct 27 4:30 AM
what ever code you are showing is the complete code..? i don't think so, show the complete logic..
‎2008 Oct 27 4:32 AM
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
‎2008 Oct 27 4:42 AM
‎2008 Oct 27 4:51 AM
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.
‎2008 Oct 27 5:08 AM
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
‎2008 Oct 27 8:19 PM
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.
‎2008 Oct 27 8:31 PM
‎2008 Oct 27 8:58 PM
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.
‎2008 Oct 27 9:03 PM
OK - do you have to read each table entry for each loop pass?
Rob
‎2008 Oct 27 9:10 PM
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.
‎2008 Oct 27 9:30 PM
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
‎2008 Oct 27 10:45 PM
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
‎2008 Oct 28 3:57 PM
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 .
‎2008 Nov 06 7:53 PM