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: 

problem in select in a loop

Former Member
0 Kudos

i m having the following scenario

loop at lit_hr into ls_hr .

select single pernr begda endda department from

pa9001 into ls_9001

where begda <= ls_hr-enda

and enda >= ls_hr-enda .

endloop .

as i cant get all the data outside the loop with for all enteries as read doesnot work with LE and GE only equal to works.

please let me know some possibility reagarding this .

Thanks

8 REPLIES 8

Former Member
0 Kudos

hiiiii

whatever u r doing is not prefer performance wise .

use for all entries ......wht kind of problen u r facing with for all entries...tell me?

Former Member
0 Kudos

Hi,

How can you now ensure that there will be only one record for this condition?

Anyway you can try in this way which is an alternative


select  pernr begda endda department from
pa9001 into table lt_9001 
for all entries in lt_hr
where begda le ls_hr-enda 
and enda ge ls_hr-enda .

loop at lit_hr into ls_hr .
loop at lt_9001 where begde le ls_hr-begda
                         and enda ge ls_hr-enda.
endloop.
endloop.

This will give multiple or single entry satisfying that condition.

Cheers,

Kothand

0 Kudos

thanks kothand but loop inside loop doesnot effect performance do let me know

thanks

0 Kudos

Hi,

Definitely No for your case. 2 reasons for this,

1) Using a LOOP inside LOOP is much better than SELECT inside a LOOP.

2) The second loop has 2 conditions. so it will not go through all the records in that table. It will go into that loop only if it satisfies these conditions.

Cheers,

Kothand

Former Member
0 Kudos

loop at lit_hr into ls_hr .

select single pernr begda endda department from

pa9001 into ls_9001

where begda <= ls_hr-enda

and enda >= ls_hr-enda .

***************

append ls_9001 to <itab>.

*********

endloop .

hope so it will eork

Former Member
0 Kudos

Select within a loop will not visibily affect performance initially if only dealing with small amounts of data, but it is not good practice as often your system grows more data over time which gradually introduces performance issues. Especially when dealing with transactional data.

Also if you do a SELECT SINGLE ensure you use the full table key, otherwise you are not guaranteed a consistent & unique set of data being returned. Plus only the EQ operator will guarantee a single record as there could be many other values LT, GT, etc.

Just a few tips

0 Kudos

so is it ok if i use loop inside loop as it will select only one record .

Former Member
0 Kudos

Done