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

select inside loop

Former Member
0 Likes
8,772

hello ,

i am new to abap as well as to this forum.pls help me....

i have code something like this..

LOOP AT ITAB.

CLEAR V_KNUMH.

SELECT SINGLE KNUMH FROM A506 INTO V_KNUMH WHERE MATNR = ITAB-MATNR.

SELECT SINGLE KBETR FROM KONP INTO ITAB-KBETR2 WHERE KNUMH = V_KNUMH.

MODIFY ITAB.

CLEAR ITAB.

ENDLOOP.

will this SELECT INSIDE LOOP affects the performance? how to avoid this?

thanx in advance..

regards,

maya

10 REPLIES 10
Read only

Former Member
0 Likes
2,278

Welcome to SDN!!!

S it will affect the performance.

Change it like

IF not itab[] is initial.
SELECT knumh from a506
             into table i_a506
             for all entries in itab
             where matnr = itab-matnr.
If sy-subrc = 0.
SELECT matnr KBETR FROM KONP 
             INTO table i_konp
             for all entries in i_a506
             WHERE KNUMH = i_a506-KNUMH.
ENDIF.
ENDIF.
LOOP AT ITAB.
READ TABLE i_konp WITH KEY ........
IF sy-subrc = 0.
itab-knmuh = i_konp-knmuh.
endif.
MODIFY ITAB.
CLEAR ITAB.
ENDLOOP.

Message was edited by:

Judith Jessie Selvi

Read only

0 Likes
2,278

thank u for ur response..

pls let me know how can i avoid this....

regards,

maya.

Read only

Former Member
0 Likes
2,278

hi Maya,

Select Single statement inside a loop will not affect much from performance point of view. .. but tbetter try using it outside the loop .

Regards,

Santosh

Read only

Former Member
0 Likes
2,278

It will effect performance

Instead of that first select all the data from A506 and KONP into 2 internal tables and then READ the data in the loop

Read only

0 Likes
2,278

Hi

Welcome to SDN.

It will defintely affects the Performance.

Try to avoid selects inside the loop for good coding .

Regards,

kumar

Read only

Former Member
0 Likes
2,278
select knumh
         matnr into table it_knumh
         from A506.

select kbetr
          knumh 
          into table it_kbetr.
          from KONP.

LOOP AT ITAB.
CLEAR V_KNUMH.
read table it_knumh with key matnr eq itab-matnr.
v_knumh = it_knumh-knumh.
read table it_kbetr with key knumh eq v_knumh.
ITAB-KBETR2 = it_kbetr-kbetr.

MODIFY ITAB.
CLEAR ITAB.
ENDLOOP.

Message was edited by:

Chandrasekhar Jagarlamudi

Read only

former_member404244
Active Contributor
0 Likes
2,278

Hi maya,

welcome to SDN

use two internal tables as itab1 and itab2 and then get the data in these tables basing on itab.and then use loop

If not itab is initial.

SELECT SINGLE KNUMH FROM A506 INTO table itab1

FOR ALL ENTRIES IN ITAB WHERE MATNR = ITAB-MATNR.

IF SY-SUBRC EQ 0.

SELECT SINGLE KBETR FROM KONP INTO ITAB2for all entries in itab1 WHERE KNUMH = itab1-KNUMH.

endif.

endif.

loop at itab.

read table itab1 with key matnr = itab-matnr.

if sy-subrc eq 0.

move : itab1-kunmh to some variable

read table itab2 with key knumh = itab1-knumh.

if sy-subrc eq 0.

move itab2-kbetr to ....

endif.

endif.

endloop.

regards,

nagaraj

Read only

Former Member
0 Likes
2,278

This will definetly affect the performance.

First you have to select the complete data into internal table.

data: begin of t_knumh occurs 0,

matnr type matnr,

knumh type knumh,

end of t_knumh.

data: begin of t_kbetr2 occurs 0,

knumh type knumh,

kbetr2 type kbetr

end of t_kbetr2.

select matnr knumh from a506 into t_knumh for all entries in itab

where matnr = itab-matnr.

select knumh kbetr from konp into t_kbetr2 for all entries in t_knumh

where knumh = t_knumh-knumh.

loop at itab.

read table t_knumh with key matnr = itab-matnr.

if sy-subrc eq 0.

read table t_kbetr2 with key knumh = t_knumh.

if sy-subrc eq 0.

move t_kbetr2-kbetr2 to itab-kbetr2.

modify itab.

endif.

endif.

endloop.

Regards,

Aravind

Read only

Former Member
0 Likes
2,278

Maya,

You can avoid this looping using the below code:

SELECT konp~kbetr INTO CORRESPONDING FIELDS OF table itab FROM konp

inner join A506 ON konpknumh = a506knumh

FOR ALL ENTRIES IN itab

WHERE a506~matnr = itab-matnr.

Thanks

Read only

Former Member
0 Likes
2,278

Hi,

A Select Statement inside a Loop definitely affects the performance. Instead try to use the following.

SELECT knumh FROM a506 into table itab_a506 FOR ALL ENTRIES IN itab

WHERE matnr = itab-matnr.

SELECT KBETR FROM konp into table itab_konp FOR ALL ENTRIES IN

itab_a506 WHERE knumh = itab_a506-knumh.

Now read the internal tables and modify the data in itab.

Regards,

Sowmya.