‎2008 Jan 04 6:57 PM
Hi Experts,
This is my sample code below
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.
In some cases, the condition LOOP AT itab2 can have >1 records in itab2 which needs to be updated to itab1. Can some one help me to add it to itab1?
thanks
Dany
‎2008 Jan 04 8:05 PM
Hi Dan,
I guess you are still working on this one. If you need records to be updated or added to itab1 based on itab2, you may want to try this. But I think giving an example would be the best way to explain the problem.
LOOP AT itab1.
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
move-corresponding itab2 to itab1.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
ENDLOOP.
ENDLOOP.
‎2008 Jan 04 7:07 PM
Hi Dan
If ITAB1 and ITAB2 has same number of records, You shouldn't worry.
instead of exiting from the loop you can modify IATB1 n the loop itself
LOOP AT itab1.
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
ENDLOOP.
ENDLOOP.
Otherwise you have to APPEND the details to a 3rd ITAB
LOOP AT itab1.
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
itab3-XXXX = itab1-XXXX
itab3-PAY_DATE = itab2-BUDAT.
itab3-PAY_AMOUNT = itab2-BETRW.
itab3-PAY_AMOUNT = itab2--BETZG.
itab3-pay_opbel = itab2-opbel.
APPEND ITAB3.
ENDLOOP.
ENDLOOP.
Edited by: Arun Shekhar on Jan 4, 2008 8:21 PM
‎2008 Jan 04 7:11 PM
Arun that doesn't work.
Oh! i forgot to mention that when a valid date comes between from date & to_date, the loop at itab2 is returing sy-subrc as 4.
itab1 & itab2 have some payment dates & amounts related fields common to each other.
Pls suggest.
‎2008 Jan 04 7:18 PM
May be this way.
data : v_flg type c.
LOOP AT itab1.
clear : v_flg.
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
if v_flg ne 'Y'.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
move 'Y' to v_flg.
else.
move-corresponding itab1 to itab3. " Here Itab3 as same structue as itab1
itab3-PAY_DATE = itab2-BUDAT.
itab3-PAY_AMOUNT = itab2-BETRW.
itab3-PAY_AMOUNT = itab2--BETZG.
itab3-pay_opbel = itab2-opbel.
append itab3
endif.
ENDLOOP.
ENDLOOP.
append lines of itab3 to itab1.
a®
‎2008 Jan 04 7:18 PM
Hi Dan,
Let us know what are the structures of itab1 and itab2 and what is the exact requirement??
‎2008 Jan 04 7:42 PM
Hi Chandrasekhar Jagarlamudi
This is with IS uitilites stuff.
The itab2 is getting popluated using an FM: FKK_READ_LAST_PAYMENTS
itab2 resembles a strcuture called FKKPAID which is as below:
The strutcture of itab2 is :
BUDAT Posting date in doc
CPUDT Accounting document
CPUTM Time of entry
OPBEL Contract Account doc no
WAERS Currency Key
BETRW payment amount
BETZG Total amount of payment
itab 1 has same & has other stuff in its strcuture. The Budat, BETRW & OPBEL need to be updated.
Now during LOOP AT itab2 WHERE budat GE itab1-from_date AND budat LE itab1-to_date.
there may be a match which has more than one records in itab2 which needs to be updated in itab1.
Right now only the first matched record in itab2 is updated.
Suggest accrodingly
‎2008 Jan 04 8:15 PM
@Dan
If for ITAB1, there exists more than 1 corresponding entry in ITAB2, you need to use ITAB3.
ITAB3 wil be the final int.Tab where in you can save your data.
ITAB3 should be of the same structure as ITAB1.
You can't go on modifying ITAB1..since if you do, only the last matching record details will be updated.
‎2008 Jan 04 8:23 PM
Hi Arun -
Seems you are close to the solution. Just tell me how do i use ITAB3 here?
thanks
‎2008 Jan 04 8:25 PM
‎2008 Jan 04 8:28 PM
Hi a®s ,
but what would be the value of v_flg during the 1st loop pass?
thanks
‎2008 Jan 04 8:32 PM
Dan,
May be this way.
data : v_flg type c.
LOOP AT itab1.
clear : v_flg. " V_flg is blank
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
if v_flg ne 'Y'. " First it will be blank after modify itab1 it will be 'Y;
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
move 'Y' to v_flg.
else. " Here v_flg will be Y so it will get append to itab3
move-corresponding itab1 to itab3. " Here Itab3 as same structue as itab1
itab3-PAY_DATE = itab2-BUDAT.
itab3-PAY_AMOUNT = itab2-BETRW.
itab3-PAY_AMOUNT = itab2--BETZG.
itab3-pay_opbel = itab2-opbel.
append itab3
endif.
ENDLOOP.
ENDLOOP.
" Loop of itab1 completes then you need to append from itab3 to itab1
append lines of itab3 to itab1.
a®
‎2008 Jan 04 8:36 PM
‎2008 Jan 04 8:38 PM
i don;t find any read statement here. Within in the loop no need to use sy-subrc
a®
‎2008 Jan 04 8:47 PM
a®s - We are using where condition in the loop at itab2 where <>..
Wont sy-subrc work here?
‎2008 Jan 04 8:53 PM
Dan,
Within loop with where condition , here sy-subrc will be always 0 no need to check
loop itab <with where condition>
" Here if your condition passes then only control will come into this
endloop.
a®
‎2008 Jan 04 9:14 PM
Hello,
I think there's a "clear" of v_flg missing in your solution, when leaving the LOOP AT itab2. Otherwise you'll have double ITAB1 entries (the original entry and a new one from ITAB3 with the additional data from ITAB2), as v_flg will be 'Y' after the first call of lopp ITAB2.
data : v_flg type c.
LOOP AT itab1.
clear : v_flg. " V_flg is blank
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
if v_flg ne 'Y'. " First it will be blank after modify itab1 it will be 'Y;
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
move 'Y' to v_flg.
else. " Here v_flg will be Y so it will get append to itab3
move-corresponding itab1 to itab3. " Here Itab3 as same structue as itab1
itab3-PAY_DATE = itab2-BUDAT.
itab3-PAY_AMOUNT = itab2-BETRW.
itab3-PAY_AMOUNT = itab2--BETZG.
itab3-pay_opbel = itab2-opbel.
append itab3
endif.
ENDLOOP.
CLEAR v_flg. "new
ENDLOOP.
" Loop of itab1 completes then you need to append from itab3 to itab1
append lines of itab3 to itab1.
Best regards
Stephan
‎2008 Jan 04 9:17 PM
Stephan,
LOOP AT itab1.
clear : v_flg. " V_flg is blank
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
you can see the v_flg is cleared after the loop at itab1.
a®
‎2008 Jan 04 9:23 PM
Hello a®s,
sorry. You're correct. I haven't seen it.
Best regards
Stephan
‎2008 Jan 04 7:20 PM
field-symbols: <wa> type itab1.
LOOP AT itab1 assigning <wa> .
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
<wa>-PAY_DATE = itab2-BUDAT.
<wa>-PAY_AMOUNT = itab2-BETRW.
<wa>-PAY_AMOUNT = itab2--BETZG.
<wa>-pay_opbel = itab2-opbel.
ENDLOOP.
ENDLOOP.
chekc this. no need do the modify.
‎2008 Jan 04 7:20 PM
did you try with in loop??
>
> Hi Experts,
> This is my sample code below
DATA : idx TYPE sy-tabix.
> LOOP AT itab1.
idx = sy-tabix.
> LOOP AT itab2 WHERE budat GE itab1-from_date
> AND budat LE itab1-to_date.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1 INDEX idx.
> ENDLOOP.
>
>
> In some cases, the condition LOOP AT itab2 can have >1 records in itab2 which needs to be updated to itab1. Can some one help me to add it to itab1?
>
> thanks
> Dany
‎2008 Jan 04 8:05 PM
Hi Dan,
I guess you are still working on this one. If you need records to be updated or added to itab1 based on itab2, you may want to try this. But I think giving an example would be the best way to explain the problem.
LOOP AT itab1.
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
move-corresponding itab2 to itab1.
itab1-PAY_DATE = itab2-BUDAT.
itab1-PAY_AMOUNT = itab2-BETRW.
itab1-PAY_AMOUNT = itab2--BETZG.
itab1-pay_opbel = itab2-opbel.
MODIFY itab1.
ENDLOOP.
ENDLOOP.
‎2008 Jan 04 8:21 PM
Hi Srinivas Adavi,
I am looking for you
That was working & Well, a new issue popped up now . sometimes there may be > 1 matching record in itab2. The current one you have posted gives sy-subrc ne 0 even for matching records.
so am using loop at itab1 where <.....>
exit.
endloop.
In your last post you have said :
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 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.
Now suggest accordingly
‎2008 Jan 04 8:36 PM
Ok, if that is the case, all you need this additional check.
loop at itab1.
loop at itab2 where <...>.
move-corresponding itab2 to itab1.
move all other fields in your original code here.
if sy-tabix = 1.
modify itab1.
else.
append itab1.
endif.
endloop.
endloop.
‎2008 Jan 04 8:45 PM
Ok, if that is the case, all you need this additional check.
loop at itab1.
loop at itab2 where <...>.
move-corresponding itab2 to itab1.
move all other fields in your original code here.
if sy-tabix = 1.
modify itab1.
else.
append itab1.
endif.
endloop.
endloop.
Srinivas,
If he append the itab1 within the loop , those records again taken for processing when loop passes, this is wrong. am i correct?
a®
‎2008 Jan 04 8:47 PM
Sorry a®s /Dan, that is true. So you do need a itab3 of type itab1.
‎2008 Jan 04 8:49 PM
So it should be this where itab3 is like itab1.
>
> Ok, if that is the case, all you need this additional check.
>
> loop at itab1.
> loop at itab2 where <...>.
> move-corresponding itab2 to itab3.
> move all other fields in your original code here to itab3 fields.
> append itab3.
> endloop.
> endloop.
‎2008 Jan 04 8:53 PM
By the way a®s, I don't see the 'code' button any more, how are you achieving indented code segments? Sorry Dan, I know this is unrelated to your issue.
‎2008 Jan 04 8:57 PM
Srinivas,
Use curly bracket code curly braket then paste the code then
curly bracket code curly braket
a®s
Edited by: a®s on Jan 4, 2008 4:00 PM
‎2008 Jan 04 9:01 PM
You can use 'code' within the curly braces like this:
[code}
put all your code lines here between the code within curly braces
to see the following:
code lines here
Hope this helps.
This is also mentioned in Suggestions thread somewhere...
Thanks
Sanjeev
‎2008 Jan 04 9:04 PM
‎2008 Jan 04 9:14 PM
Dan,
Is this working?
DATA: itab3 LIKE itab1 OCCURS 0 WITH HEADER LINE.
LOOP AT itab1.
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.
MOVE-CORRESPONDING itab2 TO itab3.
itab3-pay_date = itab2-budat.
itab3-pay_amount = itab2-betrw.
itab3-pay_amount = itab2--betzg.
itab3-pay_opbel = itab2-opbel.
APPEND itab3.
ENDLOOP.
ENDLOOP.
‎2008 Jan 04 8:28 PM
Hi Dan,
Using field symbols in such situations may reduce the complexity. With field symbols, there is no need to modify the table.
field-symbols: <fs_itab1> like line of itab1,
<fs_itab2> like line of itab2.
LOOP AT itab1 assigning <fs_itab1>.
LOOP AT itab2 assigning <fs_itab2>
WHERE budat GE <fs_itab1>-from_date
AND budat LE <fs_itab1>-to_date.
if sy-tabix eq 1. "only for the first found record of itab2
<fs_itab1>-PAY_DATE = <fs_itab2>-BUDAT.
<fs_itab1>-PAY_AMOUNT = <fs_itab2>-BETRW.
<fs_itab1>-PAY_AMOUNT = <fs_itab2>--BETZG.
<fs_itab1>-pay_opbel = <fs_itab2>-opbel.
else.
clear itab1. "use the work area for itab1
itab1-PAY_DATE = <fs_itab2>-BUDAT.
itab1-PAY_AMOUNT = <fs_itab2>-BETRW.
itab1-PAY_AMOUNT = <fs_itab2>--BETZG.
itab1-pay_opbel = <fs_itab2>-opbel.
append itab1.
endif.
ENDLOOP.
ENDLOOP.
Hope this helps.
Thanks
Sanjeev
‎2008 Jan 04 8:47 PM
Dan,
I think that you didn't see my post on page 1 at the bottom.
Please have a look at it and I think it fulfills your need.
Thanks
Sanjeev
‎2008 Jan 04 8:51 PM
Dan,
I think that you didn't see my post on page 1 at the bottom.
Please have a look at it and I think it fulfills your need.
Thanks
Sanjeev
‎2008 Jan 04 9:19 PM
Using field symbols in such situations may reduce the complexity. With field symbols, there is no need to modify the table uisng MODIFY statement.
field-symbols: <fs_itab1> like line of itab1,
<fs_itab2> like line of itab2.
data: itab_tmp like itab1.
" copy itab1 to temp table
itab_tmp[] = itab1[].
LOOP AT itab_tmp assigning <fs_itab1>.
LOOP AT itab2 assigning <fs_itab2>
WHERE budat GE <fs_itab1>-from_date
AND budat LE <fs_itab1>-to_date.
if sy-tabix eq 1. "only for the first found record of itab2
<fs_itab1>-PAY_DATE = <fs_itab2>-BUDAT.
<fs_itab1>-PAY_AMOUNT = <fs_itab2>-BETRW.
<fs_itab1>-PAY_AMOUNT = <fs_itab2>--BETZG.
<fs_itab1>-pay_opbel = <fs_itab2>-opbel.
else.
clear itab1. "use the work area for itab1
itab1-PAY_DATE = <fs_itab2>-BUDAT.
itab1-PAY_AMOUNT = <fs_itab2>-BETRW.
itab1-PAY_AMOUNT = <fs_itab2>--BETZG.
itab1-pay_opbel = <fs_itab2>-opbel.
append itab1.
endif.
ENDLOOP.
ENDLOOP.
Here itab1 is appended within the loop itself instead of appending lines from a third table later.
Thanks
Sanjeev
‎2008 Jan 04 9:27 PM
Hi Sanjeev Kumar,
You are right but we r not using fiedl symbols..and also i am fixing part of the code here.
Thanks a lot.
Dany
‎2008 Jan 04 9:29 PM
Sanjeev,
Please correct me if i am wrong
LOOP AT itab_tmp assigning <fs_itab1>.
endloop.
after this endloop appended records are in ITAB1 and modified records in ITAM_TMP. I think this not correct?
Here is pointing to .itab_tmp .
a®
‎2008 Jan 04 10:17 PM
You are right.
Actually I changed the code at the last moment. The correct one is below:
clear itab1[].
loop....
if sy-tabix eq 1. "only for the first found record of itab2
<fs_itab1>-PAY_DATE = <fs_itab2>-BUDAT.
<fs_itab1>-PAY_AMOUNT = <fs_itab2>-BETRW.
<fs_itab1>-PAY_AMOUNT = <fs_itab2>--BETZG.
<fs_itab1>-pay_opbel = <fs_itab2>-opbel.
append <fs_itab1> to itab1.
else.
....
...
endloop.
<fs_itab1> should also be appended to itab1.
Also make sure to clear the itab1 before populating it using this loop.
At the end itab1 will have correct entries.
As per Dan's requirements - The same can be achieved by replacing field-symbols by work areas. Just replace <fs_itab1> by work area itab1 and <fs_itab2> by work area itab2 and this will work.
Thanks
Sanjeev
Edited by: Sanjeev Kumar on Jan 4, 2008 5:21 PM