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

Delete from Itab

SG141
Active Participant
0 Likes
621

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
Read only

former_member194669
Active Contributor
0 Likes
580

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

Read only

Former Member
0 Likes
580

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

Read only

Former Member
0 Likes
580

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

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
580

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.

Read only

varma_narayana
Active Contributor
0 Likes
580

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>