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

Looping constructs in ABAP

Former Member
0 Likes
543

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

3 REPLIES 3
Read only

Former Member
0 Likes
517

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.

Read only

Former Member
0 Likes
517

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

Read only

Former Member
0 Likes
517

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.