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

loop statement performance

Former Member
0 Likes
1,509

DATA : LV_STBLG TYPE BKPF-STBLG.

SELECT SINGLE STBLG FROM BKPF INTO LV_STBLG WHERE BELNR = WA_FINAL-BELNR ."AND GJAHR = WA_FINAL-GJAHR.

this statement im using with in loop and im using two loops .

can u suggest me

loop at itab1

loop at itab1

conditions

endloop

loop at itab1

cond

endlloop.

DATA : LV_STBLG TYPE BKPF-STBLG.

SELECT SINGLE STBLG FROM BKPF INTO LV_STBLG WHERE BELNR = WA_FINAL-BELNR ."AND GJAHR = WA_FINAL-GJAHR.

endloop

it takes lot of time to get the output..

Thanks..

12 REPLIES 12
Read only

Former Member
0 Likes
1,056

Select statements should never be used in loops. Thats why you have FAE's.

Read only

Former Member
0 Likes
1,056

As Vikranth said, never use select statement in loops. Think about the alternatives like FOR ALL ENTRIES statement

Read only

0 Likes
1,056

Thanks for reply

Instead of using select statement how can i use the for all entries of my above condition for single filed.

Read only

Former Member
0 Likes
1,056

Use parallerl cursor technique.

Read only

0 Likes
1,056

Thanks for reply..

Could you pls provide me sample code for that. above is my conditon.

Read only

0 Likes
1,056

I do not think this select is a problem within a loop.

However, the select single cannot be met, as you haven't specified the BUKRS field.

If you execute the extended syntax check you will get a warning message for this.

Use a select xx into xxx from xxx up to 1 rows where xxxx instead.

I think the problem is your nested loops over the same table.

Why do you loop over the table three times?

How many entries do you have in this internal table?

The conditions you check, can't you filter out entries before looping?

Good luck, Miranda

Read only

0 Likes
1,056

Hi,

check [FOR ALL ENTRIES documentation|http://help.sap.com/abapdocu_70/en/ABENWHERE_LOGEXP_ITAB.htm], there is an example and explanation how does it work.

But be aware:

1. You should not use FOR ALL ENTRIES if internal table is empty, because all entries will be selected

2. You should not use FOR ALL ENTRIES if internal table contains more than approx. 1500 entries, because a select statement will be translated into a very short SQL statement and it will cause a short dump

If you are working with more than approx. 1500 entries you have to consider something else.

Ado

Read only

Former Member
0 Likes
1,056

u can use read statement within the first loop if your condition fetches just one record. use forall entries for select. below code could be helpful to you. there could be some syntax errors.

data: begin of it_stblg occurs 0,

belnr like bkpf-belnr,

stblg like bkpf-stblg,

end of it_stblg.

if it_final[] is not initial.

SELECT STBLG FROM BKPF INTO IT_STBLG

for all entries in it_final[]

WHERE BELNR = IT_FINAL-BELNR

endif.

loop at itab1 into wa_itab1_1

read table itab1 into wa_itab1_2 <with conditions>

read table itab1 into wa_itab1_3 <with conditions>

read table it_stblg with key <condition>

endloop

Read only

Former Member
0 Likes
1,056

You can also have the declaration "DATA : LV_STBLG TYPE BKPF-STBLG."outside the loop.

as this statement will executed for all the iteration of the loop.

Read only

Former Member
0 Likes
1,056

...... lots of wrong answers !!!!!!!!!!!!!!!!..

>2. You should not use FOR ALL ENTRIES if internal table contains more than approx. 1500 entries, because a select statement >will be translated into a very short SQL statement and it will cause a short dump

>If you are working with more than approx. 1500 entries you have to consider something else.

???, where does this come from, it is wrong. The limit (around 1500 but dependingon the conditions) is valid for the ranges, not for the FAE. The FAE will work with very large tables.

> Never SELECT in LOOP

is wrong ! It is wrong if there is at least one exception, this is the SELECT SINGLE from a single record buffer. There the FAE is not working

> parallel cursor

no, necessary

... Wrong is also the original question, I gues it must be


LOOP AR itab1
 ...
    LOOP at itab2

    ENDLOOP.

ENDLOOP.

The problem is not the SELECT SINGLE but the nested loop!!!!

Use a sorted table! or optimize it with READ BINARY SEARCH / LOOP FROM INDEX / EXIT (search for details).

Siegfried

Read only

0 Likes
1,056

very good answer Siegfried,

I think there were 2 much of wrong answers for the question think you cleared them all

Thanks

Nafran

Read only

Former Member
0 Likes
1,056

Hi

I beleive you can use select statement inside the loop.

because you are using 'select single' and this will fetch only one record from the buffer depending on your where condition.

So every time whenever your loop will execute it will check the condition and will give you one record.

It's like moving a values in a variable inside the loop.

like:

loop at itab into wa_itab.

variable1 = wa_itab-field1.

endloop.

here every time in a loop the value of field1 from table itab will be stored in variable1

and the variable1 should be an internal table type if you want to store multiple values.

so

loop at itab in wa_itab.

select single var1 from <tab1> into <var2> where <var3> = wa_itab<field1>.

endloop.

here for every value of <var3> = wa_itab-field1, the select statement will fetch the value of 'var1' from <tab1> and will store in <var2>.

I hope this will clear your doubt.

NOTE: we should not use SELECT END-SELECT statement inside the loop.

Thanks

Lalit Gupta