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: 

select and endselect explanatation

Former Member
0 Kudos
260

Hello experts.

LOOP AT itab.

SELECT hwbas mwskz kschl hwste txgrp buzei kbetr INTO

(itab1-hwbas, itab1-mwskz, itab1-kschl, itab1-hwste,

itab1-txgrp, itab1-buzei, bset-kbetr)

FROM bset WHERE bukrs = bukrs

AND belnr = itab-belnr

AND gjahr = itab-gjahr

AND mwskz IN mwskz .

itab1-belnr = itab-belnr.

itab1-budat = itab-budat.

itab1-bldat = itab-bldat.

itab1-xblnr = itab-xblnr.

itab1-lifnr = itab-lifnr.

itab1-gjahr = itab-gjahr.

itab1-bukrs = itab-bukrs.

itab1-kbetr = ( bset-kbetr / 10 ).

APPEND itab1.

ENDSELECT.

ENDLOOP .

I am confused , how many times in the loop for a single record the select statement will go , in my case select statement is select 5 times for a record and then it goes for the loop for another record. i dont know why it is rotating in the loop for 5 times and then going for another record.

so please explain me the code inside the loop and at what time it will go for the second time in the loop that is for another record.

Thank you so much for all the replies.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
219

Hi,

User select for all entries like

wrtite as

if itab[] is not initial.

SELECT hwbas mwskz kschl hwste txgrp buzei kbetr from bset INTO

corresponding fields of itab1

for all entries in itab

where belnr = itab-belnr

AND gjahr = itab-gjahr

AND mwskz IN mwskz .

endif.

now you loop through itab1, read corresponding itab & modify the additional fields in itab1.

7 REPLIES 7

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Kudos
219

Hi,

SELECT ENDSELECT itself is a LOOP over the database table, hence you dont need the OUTER LOOP.

So remove your LOOP ENDLOOP and use SELECT ENDSELECT only.

Even better would be to use SELECT FOR ALL ENTRIES in itab.

Regards,

Sesh

Former Member
0 Kudos
219

Hi ,

SELECT and ENDSELECT itself acts like loop , every time the query goes to the database and searches a record for the given condition in the where clause.

SO if there are 10 records which are suitable for the given condition then select query runs for 10 times.

But the code you have wrote is not good enough for performance, pleas select all the data at a strech usng for all entries and read tha data inside a loop.

if this could not be achieved,its recommended to use into table instead of SELECT and ENDSELECT as it goes into loop again inside a loop.

regards

Sunil Kumar P.

Former Member
0 Kudos
220

Hi,

User select for all entries like

wrtite as

if itab[] is not initial.

SELECT hwbas mwskz kschl hwste txgrp buzei kbetr from bset INTO

corresponding fields of itab1

for all entries in itab

where belnr = itab-belnr

AND gjahr = itab-gjahr

AND mwskz IN mwskz .

endif.

now you loop through itab1, read corresponding itab & modify the additional fields in itab1.

Former Member
0 Kudos
219

why do you want to change that coding? Is it a problem?

I don't think that any ot the above recommendation is faster. The first two are incorrect and thew last incomplete it only mentions that you must combine itab and itab1 at the end.


LOOP AT itab.

SELECT hwbas mwskz kschl hwste txgrp buzei kbetr INTO
             (itab1-hwbas, itab1-mwskz, itab1-kschl, itab1-hwste,
              itab1-txgrp, itab1-buzei, bset-kbetr)
     FROM bset WHERE bukrs = bukrs
    AND belnr = itab-belnr 
   AND gjahr = itab-gjahr
   AND mwskz IN mwskz . 

   itab1-belnr = itab-belnr.
   itab1-budat = itab-budat. 
   itab1-bldat = itab-bldat. 
   itab1-xblnr = itab-xblnr.
   itab1-lifnr = itab-lifnr.
   itab1-gjahr = itab-gjahr.
   itab1-bukrs = itab-bukrs.
   itab1-kbetr = ( bset-kbetr / 10 ).
   APPEND itab1.

  ENDSELECT.
ENDLOOP .

Your coding is difficult to read because it does not use explicit work areas and

has 2 tables itab1 and itab. It moves values from the driver table itab to itab1, for example belnr is probably the key. So the mixing of itab1 and itab will be hard, maybe impossible.

However, there is a workaround:

itab1-belnr = itab-belnr.

itab1-gjahr = itab-gjahr.

itab1-bukrs = itab-bukrs.

are not necessary they can be taken from bset. Then a combination of itab and itab1 is possible, but only faster if the tables are very large.

Siegfried

Former Member
0 Kudos
219

Hi,

Its not advisable to use SELECT & ENDSELECT but go for INTO TABLE.

Former Member
0 Kudos
219

I am confused , how many times in the loop for a single record the select statement will go , in my case select statement is select 5 times for a record and then it goes for the loop for another record. i dont know why it is rotating in the loop for 5 times and then going for another record.

so please explain me the code inside the loop and at what time it will go for the second time in the loop that is for another record.

Thank you so much for all the replies.

Hi,

1) the internal table itab may have duplicate entries so its checking the condition for all the records.

2) that may be the reason its looping inside the loop or your internal table itab may be initial.

so my suggestion for such kind of scenario is use FOR ALL ENTRIES.

as follows,

CHECK WHETHER ITAB IS NOT INITIAL.

SORT THE ITAB.

USE FOR ALL ENTRIES AND POPULATE THE ITAB1.

Reward Points if Useful

Former Member
0 Kudos
219

You should have used for all entries for optimization purpose.

Anyways answer for your question:

It is iterating five times in select endselect for every single iteration of LOOP AT itab because select-endselect is a loop that is getting executed on database for every matching condition of your where clause on select statement.

There may be 5 records matching with the where clause in the table, so select-endselect is iterating five times to get those five records.

G@urav