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: 

Deleting records in one internal table based on another one

Nik2
Explorer
0 Kudos

If i want to delete all entries in one internal tables for which entries are not there in another internal table based on one field, how to do it.

9 REPLIES 9

Former Member
0 Kudos

Hi,

Please check the code below:

LOOP AT gt_itab1 INTO gs_itab1.

READ TABLE gt_itab2 INTO gs_itab2 WITH TABLE KEY mandt = gs_itab1-mandt

lifnr = gs_itab1-lifnr

vbeln = gs_itab1-vbeln

posnr = gs_itab1-posnr

augru = gs_itab1-augru

cdate = gs_itab1-cdate

kunnr = gs_itab1-kunnr

kunwe = gs_itab1-kunwe

bstkd = gs_itab1-bstkd

docid = gs_itab1-docid

ean11 = gs_itab1-ean11

part_debit = gs_itab1-part_debit

errcode = gs_itab1-errcode.

IF sy-subrc EQ gc_zero_num.

MOVE-CORRESPONDING gs_itab2 TO gs_error1.

gs_error1-message = text-036.

APPEND gs_error1 TO gt_error1.

CLEAR gs_error1.

DELETE TABLE gt_itab2 FROM gs_itab2.

ENDIF.

ENDLOOP.

Regards

Kannaiah

Former Member
0 Kudos
loop at itab1.
  delete table itab2 where field1 ne itab1-field1.
endloop.

Former Member
0 Kudos

Hi,

Say two int tables ITAB1 and ITAB2.

Both have some common fields.

Loop at ITAB1`.

read table itab2 with key f1 = itab1-f1.

if sy-subrc <> 0.

delete itab1 index sy-tabix.

endif.

endloop.

deletes all the enteries in ITAB1 which are not present as that of ITAB2,

reward if useful

regards,

Anji

Former Member
0 Kudos

Hi Nikhil,

Considering performance also u can do this:

Sort the table to be deleted by the field on which you are performing the check.

Sort the table to be checked by the field on which you are performing the check.

Then

LOOP AT <itab_delete>.

READ TABLE <itab_check_against> WITH KEY <check_field> = <itab_delete>-<check_field> TRANSPORTING NO FIELDS.

IF SY-SUBRC <> 0.

DELETE <itab_delete> WHERE <check_field> = <itab_delete>-<check_field>.

ENDIF.

ENDLOOP.

If the <check_field> is the first field in <itab_delete> table then u can also perform the logic within the loop in a AT NEW <check_field> .... ENDAT event to improve performance (reduce number of redundant loops).

Hope this helps.

Regards,

Aditya

Nik2
Explorer
0 Kudos

dont u think delete inside a loop is a performance issue. is there any other method

aris_hidalgo
Contributor
0 Kudos

Hi,

Here is an example:

just loop through your first itab then check itab2 whether it has same entries in itab1 if not then delete.

Hope this helps...

P.S. Please award points for useful answers...

Message was edited by:

viraylab

Former Member
0 Kudos

Hi ,

If two table itab1 and itab2 to be considered

Read Table itab2 with key itab2-field1 = itab1-field1 and itab2-field2 = itab1-field2 .

If sy-subrc EQ 0 ,

DELETE itab2 where condition..... .

Endif .

Thanks ..

Former Member
0 Kudos

hi dear,

you have to delete the records of one internal table ,u hav to use the FOR ALL ENTERIES of one internal table to other for the specified key.

this will give u the robust solution.

loop at internal table 1.

delete the record for all enteries for internal table 2 with the specified key.

modify internal table one

reward points if useful

regards

Amit Singla

0 Kudos

Hi,

Deleting within loop may cause performance issue. instead you can use field-symbols.

Itab1 - from which you want to delete

itab2 - check table.

field1 - comparing field

use below code:

FIELD-SYMBOLS: <lfs_itab1> like itab1.

LOOP AT itab1 ASSIGNING <lfs-itab1>.
READ TABLE itab2 INTO wa_itab2 WITH KEY field1= <lfs_itab1>-field1.
IF sy-subrc NE 0.
CLEAR: <lfs_itab1>-field1.
ENDIF.
CLEAR: wa_itab2.
ENDLOOP.
DELETE itab1 WHERE field1 IS INITIAL.