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 Internal table in same loop- Can we do this?

Former Member
0 Kudos
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

12 REPLIES 12

former_member194669
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

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

0 Kudos

Hi,

I am getting an error message

on

DELETE ITAB FROM WA_ITAB index v_tabix

WA_ITAB May not be converted into a number.

Rob I have a situation to use this logic..

rgds

praveen

0 Kudos

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

Peranandam
Contributor

Hi,

change your statement like below specified

DELETE ITAB index v_tabix

Regards,

Peranandam

Former Member
0 Kudos

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

former_member585060
Active Contributor

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

Former Member
0 Kudos

Simply write down

DELETE ITAB.

when u have written ur delete statement.

Former Member
0 Kudos

Simple write down

DELETE ITAB.

in place of

DELETE ITAB FROM WA_ITAB.

MODIFY ITAB.

Regards,

Chintan.

former_member873340
Active Participant
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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.