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: 

Delete from Itab

SG141
Active Participant
0 Kudos
100

I want to compare two internal tables itab1 and itab2 and delete entries from itab1 where are also present in itab2.

How to do that.

5 REPLIES 5

former_member194669
Active Contributor
0 Kudos
59

Hi,


loop at itab.
v_tabix = sy-tabix.
  read table itab2 with key <<<key field >> = itab-<<<key field>>
  if sy-subrc eq 0
    delete itab index v_tabix.
  endif.
endloop.

aRs

former_member583013
Active Contributor
0 Kudos
59

LOOP AT TAB1 ASSIGNING <FS_TAB1>.
READ TABLE TAB2 WITH KEY FIELD1 = <FS_TAB1>-FIELD1.
IF SY-SUBRC EQ 0.
W_TABIX = SY-TABIX.
DELETE TAB1 INDEX W_TABIX.
ENDIF.
ENDLOOP.

Greetings,

Blag.

Message was edited by:

Alvaro Tejada Galindo

Former Member
0 Kudos
59

It is not good practice to delete internal table within loop..

use one flag in internal table,if condition is meet then flag = 'X'.

loop at itab.

v_tabix = sy-tabix.

read table itab2 with key <<<key field >> = itab-<<<key field>>

if sy-subrc eq 0

flag = 'X'.

modify itab.

endif.

endloop.

delete itab where flag = 'X'.

Thanks

Seshu

kesavadas_thekkillath
Active Contributor
0 Kudos
59

sort itab1 by < field > ascending.

sort itab2 by < field > ascending.

loop at itab1.

wk_var = itab1-<>.

read table itab2 with key <field> = wk_var binary search.

if sy-subrc = 0.

delete itab1.

endif.

clear wk_var.

endloop.

varma_narayana
Active Contributor
0 Kudos
59

Hii..

It is not Good Practice to use DELETE or APPEND on the Same ITAB within a LOOP.

Hence this can be the Right Solution for u.

**USE THIS METHOD WHEN THERE IS ONLY 1:1 MATCHING

SORT ITAB1 BY <<KEY FIELD>>

loop at ITAB2.

read table ITAB1 with key <<<key field >> = ITAB2-<<<key field>> BINARY SEARCH.

IF SY-SUBRC = 0.

v_tabix = sy-tabix.

delete itab1 index v_tabix.

endif.

endloop.

**OR YOU CAN ALSO USE... TO DELETE MULTIPLE ROWS AT A TIME

loop at ITAB2.

delete itab1 where <<KEYFIELD>> = ITAB2-KEYFIELD.

endloop.

<b>Reward if Helpful</b>