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

Problem on loop with where condition...

aris_hidalgo
Contributor
0 Likes
2,082

Hello experts,

I am wondering on why my loop statement with a where clause does not work on

some conditions but when I check on the table(it_bsis_bsas) it has those records that

is mentioned in the where clause. For example, wa_summary-hkont has a value of 1234567890

and wa_summary-year_dum has a value of 1999 and wa_summary-waers has a value of 'PHP'. I know

there are records which will satisfy it since I checked my internal table. The weird thing is

that it works on some records which also satisfies those conditions. The loop must always work

or meaning to say must always have a value of '0' in sy-subrc.

Anyway, below is my code:

DATA: lv_balance TYPE bsis-wrbtr.

FIELD-SYMBOLS: <fs_dum> LIKE LINE OF it_bsis_bsas.

it_bsis_bsas[] = im_bsis_bsas[].

it_exch[] = im_exch[].

CLEAR wa_exch.

SORT it_bsis_bsas BY hkont year_dum DESCENDING waers.

LOOP AT it_bsis_bsas ASSIGNING <fs_bsis_bsas>.

AT NEW hkont.

MOVE <fs_bsis_bsas>-hkont TO wa_summary-hkont.

ENDAT.

AT NEW year_dum.

MOVE <fs_bsis_bsas>-year_dum TO wa_summary-year_dum.

ENDAT.

AT NEW waers.

MOVE <fs_bsis_bsas>-waers TO wa_summary-waers.

t_from_curr = wa_summary-waers.

READ TABLE it_exch WITH KEY year = wa_summary-year_dum

rate = t_rate_type

from_curr = t_from_curr

INTO wa_exch TRANSPORTING exch_rate.

IF sy-subrc = 0.

MOVE wa_exch-exch_rate TO wa_summary-rate.

ENDIF.

ENDAT.

LOOP AT it_bsis_bsas ASSIGNING <fs_dum>

WHERE hkont = wa_summary-hkont

AND year_dum = wa_summary-year_dum

AND waers = wa_summary-waers.

IF sy-subrc = 0.

ADD <fs_dum>-wrbtr TO wa_summary-wrbtr.

ADD <fs_dum>-dmbtr TO wa_summary-dmbtr.

IF NOT wa_exch-exch_rate IS INITIAL.

IF <fs_dum>-waers = 'PHP'.

lv_balance = 1 / wa_exch-exch_rate * wa_exch-exch_rate.

lv_balance = <fs_dum>-wrbtr * lv_balance.

ADD lv_balance TO wa_summary-column1.

ELSE.

lv_balance = <fs_dum>-wrbtr * wa_exch-exch_rate.

ADD lv_balance TO wa_summary-column1.

ENDIF.

ENDIF.

CLEAR lv_balance.

DELETE it_bsis_bsas.

ENDIF.

ENDLOOP.

IF NOT wa_summary-column1 IS INITIAL.

APPEND wa_summary TO it_summary.

CLEAR wa_summary.

ENDIF.

ENDLOOP.

Thanks a lot guys and take care!

Message was edited by: viraylab

1 ACCEPTED SOLUTION
Read only

former_member378318
Contributor
0 Likes
1,145

Hi,

Why are you checking SY-SUBRC immediately after entering the LOOP? this makes no sense at all:

LOOP AT it_bsis_bsas ASSIGNING <fs_dum>

WHERE hkont = wa_summary-hkont

AND year_dum = wa_summary-year_dum

AND waers = wa_summary-waers.

IF sy-subrc = 0.

ADD <fs_dum>-wrbtr TO wa_summary-wrbtr.

ADD <fs_dum>-dmbtr TO wa_summary-dmbtr.

Your logic is flawed. The very fact that you have entered the loop means a matching record has been found there is no need to check sy-subrc.

If no record is found sy-subrc = 4 immediately after the loop.

4 REPLIES 4
Read only

Former Member
0 Likes
1,145

To be honest, it is not advisable to use nested loop for the same internal table. It would better if you copy the table into another one and loop at the copy with the where condition. It would surely work.

Read only

former_member378318
Contributor
0 Likes
1,146

Hi,

Why are you checking SY-SUBRC immediately after entering the LOOP? this makes no sense at all:

LOOP AT it_bsis_bsas ASSIGNING <fs_dum>

WHERE hkont = wa_summary-hkont

AND year_dum = wa_summary-year_dum

AND waers = wa_summary-waers.

IF sy-subrc = 0.

ADD <fs_dum>-wrbtr TO wa_summary-wrbtr.

ADD <fs_dum>-dmbtr TO wa_summary-dmbtr.

Your logic is flawed. The very fact that you have entered the loop means a matching record has been found there is no need to check sy-subrc.

If no record is found sy-subrc = 4 immediately after the loop.

Read only

0 Likes
1,145

Hi,

If you know that only one record would be read, I would use read table and test in sy-tabix or sy-subr.

Eddy

PS.

Put yourself on the SDN world map (http://sdn.idizaai.be/sdn_world/sdn_world.html) and earn 25 points.

Spread the wor(l)d!

Read only

Former Member
0 Likes
1,145

Hi,

problem is that you should not check

sy-subrc in loop .it will be useful to

check subrc value for read table syntax.

remove statements marked in bold

LOOP AT it_bsis_bsas ASSIGNING <fs_dum>

WHERE hkont = wa_summary-hkont

AND year_dum = wa_summary-year_dum

AND waers = wa_summary-waers.

<b>IF sy-subrc = 0.</b>ADD <fs_dum>-wrbtr TO wa_summary-wrbtr.

ADD <fs_dum>-dmbtr TO wa_summary-dmbtr.

IF NOT wa_exch-exch_rate IS INITIAL.

IF <fs_dum>-waers = 'PHP'.

lv_balance = 1 / wa_exch-exch_rate * wa_exch-exch_rate.

lv_balance = <fs_dum>-wrbtr * lv_balance.

ADD lv_balance TO wa_summary-column1.

<b>ELSE.</b>

lv_balance = <fs_dum>-wrbtr * wa_exch-exch_rate.

ADD lv_balance TO wa_summary-column1.

<b>ENDIF.</b>

ENDIF.

CLEAR lv_balance.

DELETE it_bsis_bsas.

ENDIF.

ENDLOOP.

IF NOT wa_summary-column1 IS INITIAL.

APPEND wa_summary TO it_summary.

CLEAR wa_summary.

ENDIF.

ENDLOOP.

Regards

amole