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

I need to loop through 1 table and read another until a value changes

Former Member
0 Likes
1,567

i need to read a table and sum the quantity field until a reason code changes. how do I go about doing this?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,048

You can refer to the following code for this.

LOOP AT itab INTO wa.

AT NEW region_id.

CLEAR wa_device.

ENDAT.

ENDLOOP.

Also you can use AT END OF construct similarly.

Hope this will help you.

~ Ramanath.

i need to read a table and sum the quantity field until a reason code changes. how do I go about doing this?

7 REPLIES 7
Read only

Former Member
0 Likes
1,048

sort itab by reasoncode.

Loop at itab.

quantiy = quanity + itab-quantity.

at end of reasoncode.

jtab-reasoncode = itab-reasoncodee.

jtab-sum = quantity.

append jtab.

clear quantity.

endat

endloop.

or

sort itab by reasoncode.

loop at itab.

at end of reasoncode.

sum.

jtab = itab.

append jtab.

endat.

endloop.

or

let us say itab and jtab are two tables and you want to loop through itab and read jtab for reasoncodes.

if jtab has only one entry for each entry in itab then use read else use loop.

loop at itab.

loop at jtab where reasoncode = itab-reasoncode.

quantiy = quantiy + jtab-quanity.

endloop.

endloop.

or

loop at itab.

read table jtab with key reasoncode = itab-reasoncode.

if sy-subrc eq 0.

endif

endloop.

Read only

0 Likes
1,048

I am actually loop at a differnt table and reading another like this

loop at ITAB.

read table b

Read only

0 Likes
1,048

let us say itab and jtab are two tables and you want to loop through itab and read jtab for reasoncodes.

if jtab has only one entry for each entry in itab then use read else use loop.

loop at itab.

loop at jtab where reasoncode = itab-reasoncode.

quantiy = quantiy + jtab-quanity.

endloop.

endloop.

or

loop at itab.

read table jtab with key reasoncode = itab-reasoncode.

if sy-subrc eq 0.

endif

endloop.

Read only

Former Member
0 Likes
1,048

HI,

Code like this:

loop itab1 into wa_itab1.

read table itab2 into wa_itab2 with key field2 = wa_itab1-field1.

if sy-subrc eq 0.

do the changes whatever you want.

endif.

endloop.

regards

Kannaiah

Read only

Former Member
0 Likes
1,048

Hi,

You can use AT NEW contruct inside the loop for this purpose.

Inside AT NEW reset the value of quantity else add the quantity into the variable.

Hope this will help you.

~ Ramanath.

Read only

0 Likes
1,048

do you have an example of this

Read only

Former Member
0 Likes
1,049

You can refer to the following code for this.

LOOP AT itab INTO wa.

AT NEW region_id.

CLEAR wa_device.

ENDAT.

ENDLOOP.

Also you can use AT END OF construct similarly.

Hope this will help you.

~ Ramanath.