Skip to Content
avatar image
Former Member

Delete Internal table in same loop- Can we do this?

LOOP AT ITAB INTO WA_ITAB.

read table itab2 into wa_itab2.

if sy-subrc is initial.

*I want to delete entries from ITAB.*

DELETE ITAB FROM WA_ITAB.
MODIFY ITAB.

endif.



ENDLOOP.

I am getting a dump if i do like this.

What is the correct syntax for this?

Rgds

Praveen

Edited by: PRAVEEN s on Apr 6, 2009 11:19 PM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

9 Answers

  • Apr 07, 2009 at 05:24 AM

    Hi,

    You can do that, but with little modification

    Don't use DELETE statement in LOOP and ENDLOOP statement, instead set a flag if the condition is met.

    Note:- If we use DELETEstatement in LOOP, every time the statement is executed, it has to regenerate the index, which will effect the performance if the records are more in the internal table.

    Then with DELETE statement delete all the records with the flag is set outside the LOOP.

    see the sample code

    'You have not used any where condition in READ statement, so not clear that how you are comparaing itab and itab2
    "Declare a field flag of  type c in itab internal table.
    
    
    LOOP AT itab INTO wa_itab.
      w_tabx = sy-tabix.
      READ TABLE itab2 INTO wa_itab2 WITH KEY field = wa_itab-field.
      IF sy-subrc = 0.
        wa_itab-flag = 'X'.
        MODIFY itab INDEX w_tabx FROM wa_itab TRANSPORTING flag.
      ENDIF.
      CLEAR w_tabx.
    ENDLOOP.
         
    DELETE itab WHERE flag = 'X'.

    Regards

    Bala Krishna

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 06, 2009 at 09:23 PM

    Check this

    LOOP AT ITAB INTO WA_ITAB.
    v_tabix = sy-tabix.  "<<<
    read table itab2 into wa_itab2.
     
    if sy-subrc is initial.
    DELETE ITAB FROM WA_ITAB index v_tabix.  "<<<<<.
    MODIFY ITAB.     " <<<< After deleting why need a modify ???
    endif.
    ENDLOOP.
    

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 06, 2009 at 09:24 PM

    Not a good idea to delete records from a table that you are looping through. SY-TABIX would be hard to interpret.

    Having said that, we quite often delete the particular line that we are on.

    Rob

    Edited by: Rob Burbank on Apr 6, 2009 5:32 PM

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Praveen,

      Change line DELETE ITAB FROM WA_ITAB index v_tabix.

      To DELETE TABLE ITAB FROM WA_ITAB. <----


      Use keyword 'TABLE'.

      Since 'itab' is internal table not database table.

      That is why you are getting error 'WA_ITAB May not be converted into a number.

      Edited by: Sap Fan on Apr 7, 2009 7:19 AM

  • Apr 07, 2009 at 12:06 AM

    Hi,

    change your statement like below specified

    DELETE ITAB index v_tabix

    Regards,

    Peranandam

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 07, 2009 at 05:07 AM

    Hi Praveen,

    Here in this problem , according to sytax we cant specify work area and index same time.

    so change your staemt to DELETE itab INDEX idx.

    because we are specifying index so there is no need to give work area.

    Regards,

    Nitin

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 07, 2009 at 06:06 AM

    Simply write down

    DELETE ITAB.

    when u have written ur delete statement.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 07, 2009 at 06:10 AM

    Simple write down

    DELETE ITAB.

    in place of

    DELETE ITAB FROM WA_ITAB.

    MODIFY ITAB.

    Regards,

    Chintan.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 07, 2009 at 10:24 AM

    Praveen,

    the best way to do is jus by

    DELETE ITAB where (some key ) = WA_ITAB-key.

    since itab and wa_itab has a key field this should work. Test it and telme in case you need any help.

    @ Chintan,

    Your statement will work only if the table contains a header line. If it is without header line the statement wont work.

    Gowri

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thank you all.

      As I am having issues with this delete statement with Work area.I moved it to different internal table

      and resolved issue.

      rgds

      Praveen

  • avatar image
    Former Member
    Jul 11, 2014 at 08:22 PM

    do like that

    DATA: v_tabix TYPE i.

    LOOP AT itab INTO wa_itab.

      ADD 1 TO v_tabix.

      READ TABLE itab2 INTO wa_itab2.

    IF sy-subrc IS INITIAL.

    DELETE itab INDEX v_tabix.

    SUBTRACT 1 FROM v_tabix.

    ENDIF.

    ENDLOOP.

    Add comment
    10|10000 characters needed characters exceeded