‎2007 Dec 10 6:50 PM
Hi Experts,
itab1 has from & to dates & itab2 has only budat.
I had written a loop as below:
loop at itab2 where budat <b>ge</b> itab1-from_date
and budat <b>le</b> itab1-to_date.
.......
.....
endloop.
when a valid date comes between from date & to_date, the loop is returing sy-subrc as 4. Seems something wrong in my loop condition.
can soemone correct this for me if so?
Thanks
Dany
‎2007 Dec 10 6:56 PM
Dan,
Do like this
Loop at itab2 into wa2.
if wa2-budat GE itab1-from_date and wa2-budat LE itab1-to_date.
* Do process
endif.
endloop.Regards,
Satish
‎2007 Dec 10 6:56 PM
How have you populated itab1-from_date and itab1-to_date?
Rob
‎2007 Dec 10 6:56 PM
Dan,
Do like this
Loop at itab2 into wa2.
if wa2-budat GE itab1-from_date and wa2-budat LE itab1-to_date.
* Do process
endif.
endloop.Regards,
Satish
‎2007 Dec 10 6:58 PM
hi
the loop condition seems to be fine. Not sure why you are getting sy-subrc 4. Do you have some sample data with you?
Cheers
VJ
‎2007 Dec 10 7:13 PM
Hi,
Suppose, the budat in itab2 has values '20070827' and the
itab1-from_date = 20070810
itab1-to_date = 20070910.
Here the itab2-budat definitely lies in between from & to dates but after the above loop cond is processed sy-subrc turns to be 4.
please suggest
‎2007 Dec 10 7:17 PM
You're going to have to post your code if you really want help.
Rob
‎2007 Dec 10 7:21 PM
i think the loop should work definitely. Could you post the code as Rob has suggested.
Cheers
VJ
‎2007 Dec 10 7:28 PM
Hi Rob & Vijy,
The itab2 is getting popluated using an FM: FKK_READ_LAST_PAYMENTS
itab2 resembles a strcuture called FKKPAID which is as below:
BUDAT BUDAT_KK DATS 8 Posting date in doc
CPUDT CPUDT DATS 8 0 Accounting document
CPUTM CPUTM TIMS 6 0 Time of entry
OPBEL OPBEL_KK CHAR 12 Contract Account doc no
WAERS WAERS CUKY 5 0 Currency Key
BETRW BETZU_KK CURR 13 2 payment amount
BETZG BETZG_KK CURR 13 2 Total amount of payment
*********Code**********
loop at itab1.
i_tabix = sy-tabix.
loop at payments where budat gt yt_usage-from_date
and budat le yt_usage-to_date.
end of change - GANTIH
if sy-subrc = 0.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1 INDEX I_TABIX.
ENDIF.
endloop.
endloop.
Thanks
‎2007 Dec 10 7:36 PM
As I see it from your code, you are not using itab1 dates but "yt_usage" dates. So where is the link between itab1 and itab2?
‎2007 Dec 10 7:38 PM
Hi
Oh! in order to simplify the terminology i used itab1 & itba2.
i mean : itab1 -->yt_usage
itab2 --> payments
Sorry for the inconvenience. Pls suggest
‎2007 Dec 10 7:39 PM
It makes no sense to test sy-subrc in the loop like this. It retains whatever value it had prior to entering it:
LOOP AT itab1.
i_tabix = sy-tabix.
LOOP AT payments
WHERE budat GT yt_usage-from_date
AND budat LE yt_usage-to_date.
* end of change - GANTIH
itab1-pay_date = itab2-budat.
* itab1-PAY_AMOUNT = itab2-BETRW.
itab1-pay_amount = itab2--betzg.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1 INDEX i_tabix.
ENDLOOP.
ENDLOOPYou should get rid of the nested loops as well.
Rob
‎2007 Dec 10 7:43 PM
Hi Rob
You mean sy-subrc cannot be used? please explain.
i dont think i can use "Read" here instead of loop at ..where...can you suggest an alternative?
‎2007 Dec 10 7:44 PM
Since you are updating "itab1" with the posting date based on the condition that the posting date should lie between the start and end dates, do the following.
LOOP AT itab1.
LOOP AT itab2 WHERE budat GE itab1-from_date AND budat LE itab1-to_date.
EXIT.
ENDLOOP.
IF sy-subrc = 0.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
ENDIF.
ENDLOOP.
‎2007 Dec 10 7:45 PM
Hi Dan,
The SY-SUBRC value will not change for each pass of the loop.
As rob mentioned it only has the last value before entering the loop.
Cheers
VJ
‎2007 Dec 10 7:45 PM
Once you are in the inner loop, you know that you already have matches on both tables.
To use READs instead of nested LOOPs, please see:
<a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops">The Performance of Nested Loops</a>
Rob
‎2007 Dec 10 7:46 PM
Rob - Can i use clear before checking before if sy-subrc = 0 so that the previous values are cleared from the header area?
‎2007 Dec 10 7:48 PM
Rob - No, There need not be any matching entries for both the tables
‎2007 Dec 10 7:49 PM
Not all ABAP statements set sy-subrc. The code I posted should work.
Rob
‎2007 Dec 10 7:50 PM
You may not ahve matching entries for both tables, but if you are in the inner loop, you <u>must</u> have a match.
Rob
‎2007 Dec 10 7:50 PM
Once you enter the LOOP, it means a record is found and so SY-SUBRC will always be 0 inside the LOOP. Since you are updating only one record of itab1, even if there are several records of itab2 that may satisfy the criteria of posting date between from and to dates, you can simply exit the loop as I suggested previously.
‎2007 Dec 10 7:51 PM
Hi Srinivas - Even you r checking sy-subrc = 0 ..
Then what's the difference between your code & mine?
‎2007 Dec 10 7:55 PM
If you look closely, I am checking the sy-subrc outside the inner loop (itab2 loop) which means I want to know if a record satisfying the condition is found in itab2 or not.
‎2007 Dec 10 7:57 PM
Dan - you probably should check the documentation on LOOP:
At the end of loop processing (that is after ENDLOOP), the return code value of SY-SUBRC specifies whether the loop was actually processed.
SY-SUBRC = 0:
The loop was executed at least once.
SY-SUBRC = 4:
The loop was not executed, either because there was no entry at all or because there was no entry which satisfied the conditions.
Rob
‎2007 Dec 10 7:58 PM
Also I see that FKK_READ_LAST_PAYMENTS has a I_FROM_BUDAT and a I_TO_BUDAT. So why not restrict your selection in this function call itself. Pass your itab1-from_date to I_FROM_DATE and itab1-to_date to I_TO_BUDAT.
‎2007 Dec 10 8:35 PM
Yes Srinivas - That FM is caleld in another hunk of a big program. I wil be getting the history of past 24 months and thats a different story here
‎2007 Dec 10 8:39 PM
Sirinivas - Can you just answer this piece of thead posted by you.
"Once you enter the LOOP, it means a record is found and so SY-SUBRC will always be 0 inside the LOOP. Since you are updating only one record of itab1, even if there are several records of itab2 that may satisfy the criteria of posting date between from and to dates, you can simply exit the loop as I suggested previously. "
My question - Suppose if i wanted to update more than 1 record in itab1 for which more than 1 matching entries are found then how will you write the code?
‎2007 Dec 10 8:47 PM
Let us work with an example. Let us say you have one record in itab1 with a from_date of 20070101 and to_date of 20070131. Let us say there are 3 records in itab2 with budats as 20070103, 20070128 and 20070203. Now if I am looping at itab1 and then looping at itab2 where budat is between from_date and to_date, I will get 2 records of itab2 for 1 record of itab1.
So which one should be used to update this one record of itab1? That is the issue here.
If you say it doesn't matter, then what I gave you should work.
If you say you want two records in itab1, one that has 20070103 and the other that has 20070128 as the pay_date, rest all fields of itab1 remaining the same, then your logic becomes complex, as you have to update the first record and append the second record to itab1. You may need another itab of type itab1.
‎2007 Dec 10 9:03 PM
Thanks a lot. that's terrific - Well Lastly , can you give me an idea for my scenario (1st) by avoiding the loop at where condition..etc by using reads ..as loop at where condition is a performance barrier to the program ..
‎2007 Dec 10 9:11 PM
‎2007 Dec 10 9:53 PM
Hi Rob - this is your posted code but i stil see the loop at where condition.
LOOP AT itab1.
i_tabix = sy-tabix.
LOOP AT payments
<b>WHERE budat GT</b> yt_usage-from_date
AND budat LE yt_usage-to_date.
end of change - GANTIH
itab1-pay_date = itab2-budat.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-pay_amount = itab2--betzg.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1 INDEX i_tabix.
ENDLOOP.
ENDLOOP
‎2007 Dec 10 10:00 PM
No - I meant the one with:
<a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops">The Performance of Nested Loops</a>
But now I'm not sure it's relevant with your GE and LE conditions
Rob
‎2007 Dec 10 10:00 PM