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

SELECT inside a loop ?

Former Member
0 Likes
1,237

Hello friends,

Just woundring if someone advise me something better

Actually I have a loop and inside a loop I am seleting something ( one field ). So I am using following:

Loop at lt_lfa1 into ts_lfa1.

SELECT lifnr FROM lfb1

INTO lv_lifnr

WHERE lifnr EQ ts_lfa1-lifnr

AND ....

.

Endloop.

Okey, as I am not selecting result in a table instead a field, i get error, that I have to provide endselect statement ( which I dont want ( due to performance reason). so the only solution I know is to use statement :

INTO CORRESPONDING FIELDS OF TABLE bla bla.. ( to avoid select endselect stuff). but as I am selecting only 1 field its also not convinient that I create a new internal table, ... so do any one here knows, how to not use select-endselect while selecting only one field ?

Many thanks

9 REPLIES 9
Read only

Former Member
0 Likes
1,169
use SELECT SINGLE

SELECT SINGLE lifnr FROM lfb1
INTO lv_lifnr
WHERE lifnr EQ ts_lfa1-lifnr
AND ....

Message was edited by: Sekhar

Read only

Former Member
0 Likes
1,169

Hi shah,

U can use

Loop at lt_lfa1 into ts_lfa1.

Select single lifnr FROM lfb1

INTO lv_lifnr

WHERE lifnr EQ ts_lfa1-lifnr

AND ....

Endloop.

thanks,

priya

Read only

0 Likes
1,169

hi shah,

Use <b>Select Single</b> Statement or Select * from dbtable in to table itab2 <b>for all entries</b> of itab..

Regards,

Santosh

Read only

0 Likes
1,169

thx for your replies:

Oh that was much simple, actually I am new to ABAP world , well point awared on the basis of first come, first serve!

Many thanks again

Read only

Former Member
0 Likes
1,169

Hello ,

The following code should work correctly.

DATA LT_LIFNR TYPE LIFNR OCCURS 0 WITH HEADER LINE.

SELECT LIFNR
  FROM LFB1
 INTO TABLE LT_LIFNR
  FOR ALL ENTRIES IN LT_LFA1 WHERE LIFNR = LT_LFA1-LIFNR.

LOOP AT LT_LIFNR.
  WRITE : / LT_LIFNR.
ENDLOOP.

Please do let me nkow if it worked. You just need to assign the points to this answer....:-)

Read only

Former Member
0 Likes
1,169

Hi Shah,

    • Actually I have a loop and inside a loop I am seleting something ( one field ). So I am using following:

I would suggest if you could avoid using a Select inside a loop due to performance reason. I would suggest you to make use of read statement using Binary search inside a loop.

<b>Again scenario dependendent.</b>

Suggestion steps:

1) Select the record into an internal table before loop , say itab using Array fetch.

2) Sort ur itab

3) Use Loop at lt_lfa1 into ts_lfa1.

4) In that loop used read statement along with binary search .

Using the above steps you can get read of inface Select and Endselect also.

Other wise

you can use select single inside the loop to avoid endselect , if ur reqmt. is like that only

Let me know if that helps or still if u have any doubts.

Cheers

Sunny

Rewrd points, if found helpful

Read only

0 Likes
1,169

Hello sunny,

thx for ur input, humm..intresting senario, which you have provide, will later give it a try, and will let you know, at the moment, have to move on to my current problem

regards/

Read only

Former Member
0 Likes
1,169

Hi,

Using a select statement inside a loop reduces the performance and increases the run time and the frequency of Database access .

Try this code .

select lifnr into table lt_lfb1

from lfb1

where key element is not null.

Loop at lt_lfa1 into ts_lfa1.

read table lt_lfb1 into ts_lfb1 with key lifnr eq ts_lfa1-lifnr

if sy-subrc ne 0.

clear ts_lfb1 .

endif.

endloop.

Read only

0 Likes
1,169

Hi one could also join the two tables and loop once......