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

How to avoid using SELECT inside a loop?

siongchao_ng
Contributor
0 Likes
3,721

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.

Edited by: Siong Chao on Mar 15, 2010 5:05 AM

6 REPLIES 6
Read only

Kanagaraja_L
Active Contributor
0 Likes
1,048

Select is required ?

data wa_infty_data like LINE OF t_infty_data.
LOOP AT t_pa INTO w_pa WHERE begda <= me->a_payperiod-begda AND endda >= me->a_payperiod-endda.
  MOVE w_pa-..pernr to wa_infty_data-pernr
  .... .... ....
  APPEND wa_infty_data TO t_infty_data.
ENDLOOP.

Read only

Former Member
0 Likes
1,048

Hello Siong Chao,

In the above scenario, you just have to move records from one internal table to another, for that a simple MOVE statement is needed. Select statements are meant to traverse database tables, internal tables have commands, such as READ, MOVE, MODIFY, WRITE, etc.

Regards,

Manish

Read only

Former Member
0 Likes
1,048

Hi

Put the select retrieval in outside of the loop. Use the below code.

SELECT pernr

begda

endda

INTO TABLE t_infty_data

FOR ALL ENTRIES IN t_pa

WHERE PA = t_pa-pa AND

begda <= me->a_payperiod-begda AND

AND endda >= me->a_payperiod-endda.

Then

LOOP AT t_pa INTO w_pa.

READ TABLE t_infty_data with key = w_pa-KEY

IF SY-SUVRC EQ 0.

MOVE or USE ur logic

ENDIF

ENDLOOP

Read only

Former Member
0 Likes
1,048

make sure you clear the header each time you enter the loop before reading the value from internal table through READ statement.....

Read only

0 Likes
1,048

Hi,

You can try as shown below,

data temp_pa type table of XXX (same as t_pa)

temp_pa = t_pa.

SORT temp_pa by pa.

delete adjacent duplicates temp_pa comapring pa.

loop at temp_pa into w_pa.

SELECT pernr

begda

endda

appending t_infty_data

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 table t_infty_data into wa with key....

endloop.

Note: There is a possibility of memory overflow while appending the entries into t_infty_data with data for all the different tables.

regards,

Chen

Read only

Former Member
0 Likes
1,048

Moderator message - Cross post locked Rob