Application Development 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: 

how to bring the select statement out of loop

Former Member
0 Kudos

Dear all,

Please bring out all select statements from the loop, with out affecting the original functionality.

LOOP AT s_mch1.

CONCATENATE s_mch1-low '%' INTO s_mch1-low.

SELECT matkl FROM t023 INTO r_mch1-low

WHERE matkl LIKE s_mch1-low.

r_mch1-sign = s_mch1-sign.

r_mch1-option = s_mch1-option.

APPEND r_mch1.

CLEAR r_mch1.

ENDSELECT.

ENDLOOP.

LOOP AT s_mch2.

CONCATENATE s_mch2-low '%' INTO s_mch2-low.

SELECT matkl FROM t023 INTO r_mch2-low

WHERE matkl LIKE s_mch2-low.

r_mch2-sign = s_mch2-sign.

r_mch2-option = s_mch2-option.

APPEND r_mch2.

CLEAR r_mch2.

ENDSELECT.

ENDLOOP.

LOOP AT s_mch3.

CONCATENATE s_mch3-low '%' INTO s_mch3-low.

SELECT matkl FROM t023 INTO r_mch3-low

WHERE matkl LIKE s_mch3-low.

r_mch3-sign = s_mch3-sign.

r_mch3-option = s_mch3-option.

APPEND r_mch3.

CLEAR r_mch3.

ENDSELECT.

ENDLOOP.

Thanks

Ravi

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi,

cehck this out.

data: begin of it_mch1 occurs 0,

maktl like t023-maktl,

end of it_mch1.

LOOP AT s_mch1.

CONCATENATE s_mch1-low '%' INTO s_mch1-low.

modify s_mch1.

ENDLOOP.

SELECT matkl FROM t023 INTO table it_mch1

WHERE matkl in s_mch1.

ENDSELECT.

loop at it_mch1.

r_mch1-low = it_mch1-maktl.

APPEND r_mch1.

CLEAR r_mch1.

endloop.

regards,

theja

3 REPLIES 3

Former Member
0 Kudos

hi,

cehck this out.

data: begin of it_mch1 occurs 0,

maktl like t023-maktl,

end of it_mch1.

LOOP AT s_mch1.

CONCATENATE s_mch1-low '%' INTO s_mch1-low.

modify s_mch1.

ENDLOOP.

SELECT matkl FROM t023 INTO table it_mch1

WHERE matkl in s_mch1.

ENDSELECT.

loop at it_mch1.

r_mch1-low = it_mch1-maktl.

APPEND r_mch1.

CLEAR r_mch1.

endloop.

regards,

theja

Former Member
0 Kudos

Hi Ravi,

The best way to do this would be

Better you loop at s_mch1, create another range which will contain only the low values from s_mch1.

for example,

loop at s_mch1.

r_mch-low = s_mch1-low.

append r_mch.

endloop.

SELECT matkl FROM t023 INTO r_mch1

WHERE matkl in r_mch.

Use the same logic for all the ranges.

Hope this should solve your problem

Edited by: Shobana k on Sep 17, 2008 2:00 PM

Former Member
0 Kudos

Hi Ravi,

You can use the ranges data type for this.

do the following.

Depending upon the version of ABAP you are using.

it can be

data: lr_mch1 type range of <field you want to mention>,

ls_mch1 like line of lr_mch1.

or

ranges: lr_mch1 type <field you want to mention>.

LOOP AT s_mch1 into ls_mch1.

CONCATENATE s_mch1-low '%' INTO ls_mch1-low.

APPEND ls_mch1-low to lr_mch1.

ENDLOOP.

Check the documentation of Select.. you can use IN with WHERE clause

SELECT matkl FROM t023 INTO corresponding fields of table lt_t023

WHERE matkl in lr_mch1.

and now the internal table has all the records you want without using the select statement in loop

Hope this helps.

Kinshuk