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 within loop

Former Member
0 Likes
961

Hello ,

I am trying to aviod the below select statement within a loop statement , got to know that this select cannot be avioded by other colleagues. Any suggestions?

Have also searched the forum, and got to know that not all selects within loops are avioded.

Here , inside the table t_t682i, I need entries where KSCHL = ZP17. But If I aviod the loop using READ statment,

the possibility is , I dont find any values for the first entry of ZP17 , then I have to go to second entry and get the values.


    LOOP AT t_t682i ASSIGNING <lfs_t682i>.
        SELECT  kschl
                      vkorg
                      matnr
                      knumh
                      INTO TABLE t_knumh_mrp
                      *FROM  (<lfs_t682i>-atab)*
                      FOR ALL ENTRIES IN lt_mkpf_mseg
                      WHERE kschl = zp17
                      AND vkorg = in02
                      AND matnr = lt_mkpf_mseg-matnr
                      AND ( datab LE lt_mkpf_mseg-budat AND datbi GE
                      lt_mkpf_mseg-budat ).
 ENDLOOP.

Edited by: B@SAP123 on Jul 25, 2011 2:59 PM

10 REPLIES 10
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
920

Hello,

LOOP AT t_t682i ASSIGNING <lfs_t682i>.
        SELECT  ....
                      INTO TABLE t_knumh_mrp
                      *FROM  (<lfs_t682i>-atab)*
                      FOR ALL ENTRIES IN lt_mkpf_mseg
                      WHERE kschl = zp17
                      AND vkorg = in02
                      AND matnr = lt_mkpf_mseg-matnr
                      AND ( datab LE lt_mkpf_mseg-budat AND datbi GE
                      lt_mkpf_mseg-budat ).
 ENDLOOP.

Are you selecting data dynamically from the DDIC table, whose name is maintained in <lfs_t682i>-atab ? If yes, i can't think of any logic to avoid the LOOP!

BR,

Suhas

Read only

Former Member
0 Likes
920

Hi Suhas,

Thanks for the reply.

Yes. The DDIC table name from which I need to select is in the table t_t682i (<lfs_t682i>-atab ) , I am selecting dynamically

Thanks,

Vijaya

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
920

Hello Vijaya,

Just make sure that you've done a D.A.D from t_t682i based on the field atab

SELECT inside LOOP is not suggested if you're selecting data from the same DB table. In your case it is more like selecting data from different tables(which are not known till runtime).

May be the performance gurus might have something to add.

BR,

Suhas

Read only

Former Member
0 Likes
920

Hello Suhas,

Thanks. May be basic question, but could nto get what you meant by

"" make sure that you've done a D.A.D from t_t682i based on the field atab ""

If it was to mean to SORTING, deleting the duplicates from the table , then yes, I have done that.

Edited by: B@SAP123 on Jul 25, 2011 5:10 PM

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
920

Hello Vijaya,

If it was to mean to SORTING, deleting the duplicates from the table , then yes, I have done that.

If you've deleted the duplicates from the internal table t_t682i , i don't think there should be problem.

Read only

yuri_ziryukin
Product and Topic Expert
Product and Topic Expert
0 Likes
920

I can confirm, as soon as you are selecting here from different tables, you are on a safe side.

Read only

Former Member
0 Likes
920

Hi,

Selecting data from the same table inside a loop is a performance issue.

in your case, u r selecting the details from differnt database tables which is known only at run time. i feel this is not at all a performance issue.

Read only

Former Member
0 Likes
920

hi,

Try to take join and use for all interies instead of select inside loop.

regards,

Amit

Read only

Former Member
0 Likes
920

I second gayatri. In any case this is an interesting read.

Read only

0 Likes
920

hi,

loop with read statement is better i thnk?