Application Development 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: 

How to avoid using SELECT inside a loop?

siongchao_ng
Contributor
0 Kudos
93

Hi all,

The following is my codes.

LOOP AT t_pa INTO w_pa.

SELECT pernr

begda

endda

INTO TABLE t_infty_data

FROM (w_pa-pa)

WHERE begda <= me->a_payperiod-begda

AND endda >= me->a_payperiod-endda.

Anyone have any idea how can perform the Select statement outside the loop? I used the loop because at program run tme, I do not know yet which database tables to select from. Thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
60

Hi,

if you know, that the select uses only one table, than you can create SELECT with FOR ALL ENTRIES before the LOOP statement.

If T_PA-PA contains different tables than you can do the select only inside the loop - because in the worst situation, you query one different table for each row of T_PA table.

Regards,

--

Przemysław

2 REPLIES 2

Former Member
0 Kudos
61

Hi,

if you know, that the select uses only one table, than you can create SELECT with FOR ALL ENTRIES before the LOOP statement.

If T_PA-PA contains different tables than you can do the select only inside the loop - because in the worst situation, you query one different table for each row of T_PA table.

Regards,

--

Przemysław

former_member641978
Active Participant
0 Kudos
60

Hi


t_pa2[] = t_pa[]

sort t_pa2 by pa.
delete adjusting duplicates comparing pa.

LOOP AT t_pa2 INTO w_pa.
SELECT pernr
begda
endda
APPENDING TABLE t_infty_data *-------> add one more field with name of (w_pa-pa) by code as you want
FROM (w_pa-pa)
WHERE begda <= me->a_payperiod-begda
AND endda >= me->a_payperiod-endda.
endloop.

LOOP AT t_pa INTO w_pa.
read t_infty_data.... ---> first key is name of table (w_pa-pa).
.......
...........
........
endloop.

This way you make only one select for each table

Best Regards

Yossi Rozenberg

Best Regards

Yossi Rozenberg