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 with nested Loop

Former Member
0 Likes
522

Hi Experts,

i ve problem in nested loop. my peace of code is below.

CLEAR: it_prps, it_final, wa_final, wa_it_prps.

LOOP AT it_prps.

CASE month.

WHEN '08'.

      • ACTUAL BILLING

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_04

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_coin OR vorga = c_rku2 ).

CLEAR act_billing.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_act_billing.

act_billing = act_billing + t_act_billing.

ENDIF.

  • CLEAR: it_prps, it_final.

CLEAR: wa_final.

ENDLOOP.

      • PLANNED REVENUE

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_01

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_sdor OR vorga = c_rkp5 ).

CLEAR plan_rev.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_plan_rev.

plan_rev = plan_rev + t_plan_rev.

ENDIF.

  • CLEAR: it_prps, it_final.

ENDLOOP.

      • PLANNED COST

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_01

AND versn = c_0

AND beltp = c_1.

CLEAR plan_cost.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_plan_cost.

plan_cost = plan_cost + t_plan_cost.

ENDIF.

CLEAR: wa_final.

ENDLOOP.

ENDCASE.

ENDLOOP.

In the above code am trying to find 3values like Actual Billing, Planned Revenue and Planed cost.

Under the main loop Loop at it_prps. here am looping the table it_final 3times for finding 3values.

but the problem is if i comment for below 2 loops, then First loop is doing well. but if i open for 3 loops

now supose if First loop fails then control is come out of main loop( it_prps ), it is going to 2nd and 3rd loops. and its giving zero values.

Could anyone tell me where i gone rong, how control will come to 2nd loop if 1st loop fails.

Thanks in Advance,

sudharsan.

Edited by: SUDHARSAN RAO on May 6, 2008 3:39 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
497

Number of issues with this code.

First.. how is it_final defined. With a header? or standard table of wa_final?

If like the 2nd (standard table of wa_final) then this line will clear the table. CLEAR: it_prps, it_final.

Other observations.

In each of your internal loops that have the pattern like this


      LOOP AT it_final INTO wa_final
      WHERE posid = it_prps-posid
      AND wrttp = c_04
      AND beltp = c_02
      AND versn = c_0
      AND ( vorga = c_coin OR vorga = c_rku2 ).

        CLEAR act_billing.
        IF sy-subrc IS INITIAL.
          MOVE wa_final-wlp08 TO t_act_billing.
          act_billing = act_billing + t_act_billing.
        ENDIF.

        CLEAR: it_prps, it_final.
        CLEAR: wa_final.
      ENDLOOP.

Should be recoded to look like this


      CLEAR act_billing.    "<== Moved
      LOOP AT it_final INTO wa_final
      WHERE posid = it_prps-posid
      AND wrttp = c_04
      AND beltp = c_02
      AND versn = c_0
      AND ( vorga = c_coin OR vorga = c_rku2 ).

**        IF sy-subrc IS INITIAL.   " Don't Need this.. will only come in here if record found
          MOVE wa_final-wlp08 TO t_act_billing.
          act_billing = act_billing + t_act_billing.
**        ENDIF.      " Matches the IF

**        CLEAR: it_prps, it_final.   " Shouldn't need this
 **       CLEAR: wa_final.            " Shouldn't need this
      ENDLOOP.

3 REPLIES 3
Read only

Former Member
0 Likes
497

Hi.

I think you should a flag to check the process in each for loops.

thats all..

Read only

Former Member
0 Likes
498

Number of issues with this code.

First.. how is it_final defined. With a header? or standard table of wa_final?

If like the 2nd (standard table of wa_final) then this line will clear the table. CLEAR: it_prps, it_final.

Other observations.

In each of your internal loops that have the pattern like this


      LOOP AT it_final INTO wa_final
      WHERE posid = it_prps-posid
      AND wrttp = c_04
      AND beltp = c_02
      AND versn = c_0
      AND ( vorga = c_coin OR vorga = c_rku2 ).

        CLEAR act_billing.
        IF sy-subrc IS INITIAL.
          MOVE wa_final-wlp08 TO t_act_billing.
          act_billing = act_billing + t_act_billing.
        ENDIF.

        CLEAR: it_prps, it_final.
        CLEAR: wa_final.
      ENDLOOP.

Should be recoded to look like this


      CLEAR act_billing.    "<== Moved
      LOOP AT it_final INTO wa_final
      WHERE posid = it_prps-posid
      AND wrttp = c_04
      AND beltp = c_02
      AND versn = c_0
      AND ( vorga = c_coin OR vorga = c_rku2 ).

**        IF sy-subrc IS INITIAL.   " Don't Need this.. will only come in here if record found
          MOVE wa_final-wlp08 TO t_act_billing.
          act_billing = act_billing + t_act_billing.
**        ENDIF.      " Matches the IF

**        CLEAR: it_prps, it_final.   " Shouldn't need this
 **       CLEAR: wa_final.            " Shouldn't need this
      ENDLOOP.

Read only

Former Member
0 Likes
497

Hi Sudarshan,

Check the below code....

LOOP AT it_prps.

CASE month.

WHEN '08'.

LOOP AT it_final INTO wa_final

posid = it_prps-posid.

IF wrttp = c_04

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_coin OR vorga = c_rku2 ).

*Do the process for ACTUAL BILLING

ENDIF.

IF wrttp = c_01

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_sdor OR vorga = c_rkp5 ).

*Do the process for PLANNED REVENUE

ENDIF.

IF wrttp = c_01

AND versn = c_0

AND beltp = c_1.

CLEAR plan_cost.

*Do the process for PLANNED COST

ENDIF.

CLEAR: it_prps, it_final.

CLEAR: wa_final.

ENDLOOP.

ENDCASE.

ENDLOOP.

Cheers,

Bujji