‎2007 Sep 12 7:15 PM
Hi,
I have seen many times as first records are fetched using select statement and then they are manipulated and displayed using LOOP...ENDLOOP. and sometimes first LOOP...ENDLOOP is implemented and within it there exists a OPEN SQL statement to fetch the resultset to be manipulated or processed.
Which approach is widely used or what are the situations in which former and later approach is used.
Thanks and Regards
Ameet
‎2007 Sep 12 7:18 PM
both are same,,
but first approach is good compare to second one.bec performance wise
first we select the data and process it in loop and enloop.
in case of second apporche system will take lot of time to select the
data with in a loop.
‎2007 Sep 12 7:22 PM
The first approach is always better.
First select all the data you REQUIRE from the database. Once the data is available in the application server you can use the LOOP to process the data.
The second approach is a big Performance issue. Each pass of the loop the system will go to database. Try avoiding the second option.
Cheers
VJ
‎2007 Sep 12 9:16 PM
Hi Ameet,
I prefer doing select at first then loop the values to process data further.
It will cause additional time for some cases,
but the second method will ALWAYS add some unneeded times to your programs.
Using SELECT from inside of LOOP is the most cause of similar queries.
Try the following codes, then compare the trace result of both method using ST05.
data :
begin of it_vbap occurs 1000,
vbeln like vbap-vbeln,
posnr like vbap-posnr,
matnr like vbap-matnr,
maktx like makt-maktx,
end of it_vbap,
begin of it_makt occurs 1000,
matnr like makt-matnr,
maktx like makt-maktx,
end of it_makt,
it_vbapm like it_vbap occurs 1000 with header line.
select vbeln posnr matnr from vbap into it_vbap up to 1000 rows.
loop at it_vbap.
select single maktx into it_vbap-maktx from makt
where matnr = it_vbap-mantr.
modify it_vbap.
endloop.
break-point.
select vbeln posnr matnr from vbap into it_vbap up to 1000 rows.
sort it_vbap by matnr.
it_vbapm[] = it_vbap[].
delete adjacent duplicates from it_vbapm comparing matnr.
if it_vbapm[] is not initial.
select matnr maktx from makt into table it_makt
for all entries of it_vbapm where matnr = it_vbapm-matnr.
sort it_makt by matnr.
endif.
loop at it_vbap.
read table it_makt with key matnr = it_vbap-matnr binary search.
if sy-subrc = 0.
it_vbap-maktx = it_makt-maktx.
modify it_vbap.
endif.
endloop.