2008 Mar 25 10:59 AM
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.
2008 Mar 25 11:51 AM
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.
2008 Mar 25 11:07 AM
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
2008 Mar 25 11:11 AM
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.
2008 Mar 25 11:51 AM
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.
2008 Mar 25 1:20 PM
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
2008 Mar 26 4:38 AM
Hi,
Its not advisable to use SELECT & ENDSELECT but go for INTO TABLE.
2008 Mar 27 7:33 AM
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
2008 Mar 31 6:42 PM
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