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 select query in loop

Former Member
0 Likes
649

Hi All,

I have to select entries from table iclpay looping it_dfkkop and delete the entries in it_dfkkop_tmp which are not present in iclpay.

For this If use below code its working fine, but there is performance issue

CLEAR: it_iclpay[],it_iclpay.

LOOP AT it_dfkkop_tmp.

SELECT claim documentref paycat status FROM iclpay

APPENDING CORRESPONDING FIELDS OF TABLE it_iclpay

WHERE

active = c_active AND

claim = it_dfkkop_tmp-claim AND

documentref = it_dfkkop_tmp-opbel.

IF sy-subrc NE 0.

DELETE it_dfkkop_tmp.

ENDIF.

ENDLOOP.

If I write the code like below its not working

SELECT claim documentref paycat status FROM iclpay

INTO TABLE it_iclpay

FOR ALL ENTRIES IN it_dfkkop_tmp WHERE

active = c_active AND

claim = it_dfkkop_tmp-claim AND

documentref = it_dfkkop_tmp-opbel.

Can anyone help on this.

5 REPLIES 5
Read only

Former Member
0 Likes
608

What do you mean by not working? The purpose of using FOR ALL ENTRIES is to avoid select query within loop. Are you getting any error or dump with your second select query?

Read only

0 Likes
608

It did not dump but did not get final(required) output.

I got the solution now.

Thanks a lot for responding.

I have written the below code

SELECT claim

documentref

paycat

status

FROM

iclpay

APPENDING CORRESPONDING FIELDS OF TABLE it_iclpay

FOR ALL ENTRIES IN it_dfkkop_tmp WHERE

active = c_active AND

claim = it_dfkkop_tmp-claim AND

documentref = it_dfkkop_tmp-opbel.

SORT it_dfkkop_tmp BY claim opbel.

LOOP AT it_dfkkop_tmp.

READ TABLE it_iclpay INTO wa_iclpay WITH KEY

claim = it_dfkkop_tmp-claim

documentref = it_dfkkop_tmp-opbel.

IF sy-subrc NE 0.

DELETE it_dfkkop_tmp.

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
608

Answered

Read only

Former Member
0 Likes
608

if it selects nothing, then there probably nothing to delete.

Your FAE looks fine to me:

What you need is:

define itab1 as SORTED TABLE ...


if NOT ( itab IS INITIAL).
  SELECT 
       INTO TABLE itab1
       ...
       FOR ALL ENTRIES itab0
       ... 
ENDIF.

if NOT ( itab1 IS INITIAL).
  LOOP AT itab0 INTO wa0.
     READ TABLE itab1 WITH KEY ...
     IF ( sy/subrc ne 0 ).
       APPEND wa TO itab2.
      ENDIF.
   ENDLOOP.
   DELETE dbtab FROM TABLE itab2.
  ENDIF.
ELSE
   DELETE dbtab FROM TABLE itab0.
ENDIF.

details are not hard to figure out!

Read only

Former Member
0 Likes
608

Answered

only half