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

loop within loop

Former Member
0 Likes
1,311
LOOP AT gt_final INTO gs_final.
LOOP AT gt_i_shipment INTO gs_i_shipment 
WHERE vbeln =  gs_final-vbeln  AND posnr = gs_final-posnr.

Eventhough i have corresponding value in gt_i_shipment.for some values it reads for some it doesnt.SY-SUBRC returns non zero.its not reading for some entries .any solutions please.

1 ACCEPTED SOLUTION
Read only

former_member404244
Active Contributor
0 Likes
1,260

Hi,

the code seems ok.. Might be in some cases the vblen or posnr might have leading zero's but in the other table not having them something like this.. check care fully the values in the two internal tables.

Regards,

Nagaraj

14 REPLIES 14
Read only

former_member404244
Active Contributor
0 Likes
1,262

Hi,

the code seems ok.. Might be in some cases the vblen or posnr might have leading zero's but in the other table not having them something like this.. check care fully the values in the two internal tables.

Regards,

Nagaraj

Read only

0 Likes
1,260

i recommend you to set a breakpoint in the second loop... and check where conditions at runtime...

that will help you to find the problem!!

Read only

0 Likes
1,260

Hi

data is same in both the cases.Should i sort before loop.?

Read only

0 Likes
1,260

no... sorting just will help on perfomance...

mmm. if you are sure that data is equal in both tables... mmmm maybe you should check data elements ... must be the same too

Read only

MichiFr
Participant
0 Likes
1,260

Actually you didn't show us the code before the two ENDLOOP statements. May be you are changing one or both of the two loop tables? Are you deleting any of the records from any internal table?

Are you sure that the formatting of VBELN and POSNR is the always the same with leading zeros and so on? It may be that any conversion exit hides this kind of information in debugger.

Read only

Former Member
0 Likes
1,260

THIS IS MY CODE

 LOOP AT gt_final INTO gs_final.
LOOP AT gt_i_shipment INTO gs_i_shipment 
WHERE vbeln =  gs_final-vbeln  AND posnr = gs_final-posnr.
      
      gs_final-knota       =  gs_i_shipment-knota .
      gs_final-shtyp       =  gs_i_shipment-shtyp .
      gs_final-wadat       =  gs_i_shipment-wadat .
      gs_final-dplen       =  gs_i_shipment-dplen .
    MODIFY  gt_final FROM gs_final.
    CLEAR : gs_i_shipment,gs_kna1,gs_jest.
    ENDLOOP.

    CLEAR :gs_final.
  ENDLOOP. 

Read only

Former Member
0 Likes
1,260

maybe the entries that doesnt satisfied the conditions are the ones with sy-subrc <> 0???

Read only

Former Member
0 Likes
1,260

But you didn't show where you are checking sy-subrc!!

Rob

Read only

Former Member
0 Likes
1,260
LOOP AT gt_final INTO gs_final.
LOOP AT gt_i_shipment INTO gs_i_shipment 
WHERE vbeln =  gs_final-vbeln  AND posnr = gs_final-posnr.
     check sy-subrc = 0. <-----------------------------------------Here i AM CHECKING
      gs_final-knota       =  gs_i_shipment-knota .
      gs_final-shtyp       =  gs_i_shipment-shtyp .
      gs_final-wadat       =  gs_i_shipment-wadat .
      gs_final-dplen       =  gs_i_shipment-dplen .
    MODIFY  gt_final FROM gs_final.
    CLEAR : gs_i_shipment,gs_kna1,gs_jest.
    ENDLOOP.
 
    CLEAR :gs_final.
  ENDLOOP. 
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,260

Hello,

Are you sure the value of SY-SUBRC is set in the LOOP ... WHERE block. I read the documentation & do not find it mentioned there. Check what is the value of SY-SUBRC before the LOOP statement.

Read only

Former Member
0 Likes
1,260

about loop within loop , you can refer to the following code:

Method One:

  • Entries: 100 (ITAB1), 1000 (ITAB2)

  • Line width: 100

  • Both tables sorted by key K

LOOP AT ITAB1 INTO WA1.

LOOP AT ITAB2 INTO WA2

WHERE K = WA1-K.

" ...

ENDLOOP.

ENDLOOP.

Method Two:

  • Entries: 100 (ITAB1), 1000 (ITAB2)

  • Line width: 100

  • Both tables sorted by key K

I = 1.

LOOP AT ITAB1 INTO WA1.

LOOP AT ITAB2 INTO WA2 FROM I.

IF WA2-K <> WA1-K.

I = SY-TABIX.

EXIT.

ENDIF.

" ...

ENDLOOP.

ENDLOOP.

the method two is faster than the method one.

Read only

Former Member
0 Likes
1,260

HI

I beleive you should use Read statement inside the loop.

like:

loop at gt_final into gs_final.

Read table gt_i_shipment into gs_i_shipment

where vbeln = gs_final-vbeln and posnr = gs_final-posnr.

gs_final-knota = gs_i_shipment-knota .

gs_final-shtyp = gs_i_shipment-shtyp .

gs_final-wadat = gs_i_shipment-wadat .

gs_final-dplen = gs_i_shipment-dplen .

MODIFY gt_final FROM gs_final.

endloop.

this will improve the performance of your code.

because you are already applying loop on gt_final and checking the vbeln and posnr from gt_final, so there is no need of applying llop at gt_i_shipment.

I hope this will work for you.

Thanks

Lalit Gupta

Read only

Former Member
0 Likes
1,260

It was My Mistake

Actaully answer is

At the end of loop processing (i.e. 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.

Read only

0 Likes
1,260

HI

just apply If sy-subrc = 0 after reading the table .

see below:

loop at gt_final into gs_final.

Read table gt_i_shipment into gs_i_shipment

where vbeln = gs_final-vbeln and posnr = gs_final-posnr.

if sy-subrc = 0.

then

move your fields.

in this case only the values will be moved if there is a matching value of vbeln and posnr.

no need to check sy-subrc after the endloop statement.

Thanks

Lalit Gupta