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 ITAB

Former Member
0 Kudos

Hello friends,

I have an Internal table and it is getting some duplicate values. For example,

I have the same values in line 3, 6 and 9 of the internal table ( All fields have the same value)

I want to delete Line 6 and 9 from the internal table. However i dont want to sort the Internal table.

Any suggestions,

Madhu.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Check this..

DATA: BEGIN OF ITAB OCCURS 0,

VALUE,

END OF ITAB.

DATA: V_INT TYPE SYTABIX.

ITAB-VALUE = '1'. APPEND ITAB.

ITAB-VALUE = '2'. APPEND ITAB.

ITAB-VALUE = '3'. APPEND ITAB.

ITAB-VALUE = '1'. APPEND ITAB.

ITAB-VALUE = '4'. APPEND ITAB.

ITAB-VALUE = '3'. APPEND ITAB.

LOOP AT ITAB.

CLEAR: V_INT.

LOOP AT ITAB WHERE VALUE = ITAB-VALUE.

V_INT = V_INT + 1.

IF V_INT > 1.

DELETE ITAB.

ENDIF.

ENDLOOP.

ENDLOOP.

Thanks,

Naren

12 REPLIES 12

amit_khare
Active Contributor
0 Kudos

Hi,

You can use index number to delete rows.

DELETE itab INDEX sy-tabix.

Regards,

Amit

Former Member
0 Kudos

Use DELETE itab INDEX idx.

Thanks,

Santosh

0 Kudos

how can i dynamically figure the index number.

Any example please.

Madhu

0 Kudos

Thats a tricky thing as you you can check out which is a duplicate row in Itab just store the index number of that row in a variable and pass it to delete.

But this require a very dynamic and tricky programming, thats why it is prefered to sort and delete. this even save a lot on performance of the report too.

Regards,

Amit

0 Kudos

Use this:

data: lv_index like sy-index.

loop at itab.

lv_index = sy-index.

DELETE itab INDEX lv_index.

endloop.

Thanks,

Santosh

0 Kudos

You have to process the ITAB for each 'n every duplicate value.

e.g.

LOOP at ITAB into wa_itab1.

loop at itab into wa_itab2.

Check wa_itab1 = wa_itab2.

DELETE ITAB from wa_itab1.

ENDLOOP.

ENDLOOP.

it miet bthe ogic. just try it ou.

by d way y don't want to SORT the ITAB.

this can be asilydone using SORT 'n DELTE ADAJCENT

hope this will help

Ashwani

0 Kudos

Thanks All,

I will try the logic suggested.

However the order is very important here. Its basically the IDOC segments, So I cant sort. One more important thing is when I am deleting the ITAB i cant delete the first hit record. As i said in my post from line 3, 6 and 9 i cant delete 3.

THanks,

Madhu.

former_member194669
Active Contributor
0 Kudos

Hi,

Eg. Itab has your internal table

itab1[] = itab[].

sort itab1 by <your field name >

delete adjacent duplicates from itab1 comparing <your field name>

loop at itab1.

read table itab with key <field Name > = itab1-<fieldname>

if sy-subrc eq 0

delete itab index sy-tabix.

endif.

endloop.

After this loop itab contain the records as you mentioned.

ie Every first entry in itab of the key has been deleted.

aRs

Former Member
0 Kudos

Hi,

Check this..

DATA: BEGIN OF ITAB OCCURS 0,

VALUE,

END OF ITAB.

DATA: V_INT TYPE SYTABIX.

ITAB-VALUE = '1'. APPEND ITAB.

ITAB-VALUE = '2'. APPEND ITAB.

ITAB-VALUE = '3'. APPEND ITAB.

ITAB-VALUE = '1'. APPEND ITAB.

ITAB-VALUE = '4'. APPEND ITAB.

ITAB-VALUE = '3'. APPEND ITAB.

LOOP AT ITAB.

CLEAR: V_INT.

LOOP AT ITAB WHERE VALUE = ITAB-VALUE.

V_INT = V_INT + 1.

IF V_INT > 1.

DELETE ITAB.

ENDIF.

ENDLOOP.

ENDLOOP.

Thanks,

Naren

Former Member
0 Kudos

hi

You can delete the multiple records from database table by putting all the records which u want to delete in internal table.

DELETE SMARA FROM TABLE ITAB.

in this case whatever you have in internal table will be deleted from SMARA.

append the internal table.

else try this :

SORT itab ASCENDING BY xxx.

DELETE ADJACENT DUPLICATES FROM itab COMPARING xx.

else : You can solve your prob using below code:

DATA: ind(1), p_val like itab-field1.

SORT itab ASCENDING field1.

LOOP AT itab INTO wa_itab.

CLEAR: p_val, ind.

ON CHANGE OF field1.

p_val = itab-field1.

ind = 'X'.

ENDON.

IF itab-field1 = p_val AND NOT ind IS INITIAL.

DELETE itab INDEX sy-tabix.

ENDIF.

ENDLOOP.

sri

award points if helpful

Former Member
0 Kudos

Hi,

Please check my reply..

Thanks,

Naren

0 Kudos

Thanks Naren.

Madhu.