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

Is this loop cond correct?

Former Member
0 Likes
2,280

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,253

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

31 REPLIES 31
Read only

Former Member
0 Likes
2,253

How have you populated itab1-from_date and itab1-to_date?

Rob

Read only

Former Member
0 Likes
2,254

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

Read only

Former Member
0 Likes
2,253

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

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

You're going to have to post your code if you really want help.

Rob

Read only

0 Likes
2,253

i think the loop should work definitely. Could you post the code as Rob has suggested.

Cheers

VJ

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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?

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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.
ENDLOOP

You should get rid of the nested loops as well.

Rob

Read only

0 Likes
2,253

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?

Read only

0 Likes
2,253

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.

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

Rob - Can i use clear before checking before if sy-subrc = 0 so that the previous values are cleared from the header area?

Read only

0 Likes
2,253

Rob - No, There need not be any matching entries for both the tables

Read only

0 Likes
2,253

Not all ABAP statements set sy-subrc. The code I posted should work.

Rob

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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.

Read only

0 Likes
2,253

Hi Srinivas - Even you r checking sy-subrc = 0 ..

Then what's the difference between your code & mine?

Read only

0 Likes
2,253

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.

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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.

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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?

Read only

0 Likes
2,253

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.

Read only

0 Likes
2,253

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 ..

Read only

0 Likes
2,253

Dan - check my earlier post.

Rob

Read only

0 Likes
2,253

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

Read only

0 Likes
2,253

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

Read only

Former Member
0 Likes
2,253

Done.. Thanks to all