Skip to Content
author's profile photo Former Member
Former Member

Deleting records in one internal table based on another one

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.

Add a comment
10|10000 characters needed characters exceeded

  • 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.

Assigned Tags

Related questions

8 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 09:15 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 09:17 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 09:18 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 09:26 AM

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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 23, 2007 at 09:33 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 09:38 AM

    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 ..

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 10:09 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 23, 2007 at 09:17 AM
    -5
    loop at itab1.
      delete table itab2 where field1 ne itab1-field1.
    endloop.
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.