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 statement

Former Member
0 Kudos

Hello Friends,

I want to delete records comparing one field for duplicates,

I am using

DELETE ADJACENT DUPLICATES FROM it_knvp COMPARING kunn2.

But I want to delete all the records if duplicate exist, but the above statement is keeping one record in the internal table and deleting the rest for duplicates.

I am thinking of reading the internal table into a work area and delete the internal table.

but is there a easier way to delete it.

Let me know for any questions,

Shejal Shetty.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

You can do one thing...

sort itab1 by kunn2.

itab2[] = itab1[].

sort itab2 by kunn2.

loop at itab1.

clear cnt.

loop at itab2 with key kunn2 = itab1-kunn2.

cnt = cnt + 1.

endloop.

if cnt >= 2.

delete itab1 where kunn2 = itab2-kunn2.

endif.

endloop.

itab1 will have the resultant.

Regards,

Raj

Message was edited by: Rajasekhar Dinavahi

Message was edited by: Rajasekhar Dinavahi

13 REPLIES 13

former_member404244
Active Contributor
0 Kudos

Hi Shejal,

u can do like this

DELETE ADJACENT DUPLICATES FROM it_knvp COMPARING all fields.

Regards,

Nagaraj

Former Member
0 Kudos

Hi,

you can try it this way :

sort it_knvp on kunn2.

delete adjacent duplicates from it_knvp .

regards,

Shikha

Former Member
0 Kudos

Hi

You can do one thing...

sort itab1 by kunn2.

itab2[] = itab1[].

sort itab2 by kunn2.

loop at itab1.

clear cnt.

loop at itab2 with key kunn2 = itab1-kunn2.

cnt = cnt + 1.

endloop.

if cnt >= 2.

delete itab1 where kunn2 = itab2-kunn2.

endif.

endloop.

itab1 will have the resultant.

Regards,

Raj

Message was edited by: Rajasekhar Dinavahi

Message was edited by: Rajasekhar Dinavahi

0 Kudos

Thanks all,

Thangeswaran I want to delete all the records only if duplicate exist . However if duplicates does not exist I dont want to delete that single record.

Shejal.

0 Kudos

Hi

heve you seen my sample in previuos post:

SORT IT_KNVP BY KUNN2.

  • Save data

IT_KNVP_COPY[] = IT_KNVP[].

  • Delete duplicate keys

DELETE ADJACENT DUPLICATES FROM IT_KNVP COMPARING KUNN2.

  • Check which records have a duplicate key

LOOP AT IT_KNVP.

COUNT = 0.

LOOP IT_KNVP_COPY WHERE KUNN2 = IT_KNVP-KUNN2.

COUNT = COUNT + 1.

ENDLOOP.

IF COUNT > 1.

  • Here delete last record

DELETE IT_KNVP.

DELETE IT_KNVP_COPY WHERE KUNN2 = IT_KNVP-KUNN2.

ENDIF.

ENDLOOP.

Max

0 Kudos

Hi Shejal,

Small change in above code:

<b>if it_knvp_prev-kunn2 eq it_knvp-kunn2.</b>

instead of if it_knvp_prev eq it_knvp.

Regards,

Vivek

0 Kudos

U can even try the below...

sort itab by kunn2.

loop at itab.

at new kunn2.

lctr = 0.

lkunn2 = itab-kunn2.

endat.

lctr = lctr + 1.

at end of kunn2.

delete itab where kunn2 = lkunn2.

endat.

Endloop.

U can use on change even if kunn2 is not your first field of internal table itab.

0 Kudos

Hi,

DELETE ADJACENT DUPLICATES FROM it_knvp COMPARING kunn2.
IF sy-subrc = 0.
  IF NOT it_knvp[] IS INITIAL.
    DELETE it_knvp FROM 1.
  ENDIF.
ENDIF

In the above code, <b>if sy-subrc = 0</b> is used to check that condition...

So, first it deletes the duplicate records.

If duplicate records are deleted, then all the records will be deleted from it_knvp.

otherwise, it will not delete that single record.

Hope this helps!

best regards,

Thangesh

0 Kudos

Thanks Everyone,

Thanks Thangeswaran. It works,

Shejal Shetty.

Former Member
0 Kudos

Hi,

kindly check the following code:

DELETE ADJACENT DUPLICATES FROM it_knvp COMPARING kunn2.
IF sy-subrc = 0.
  IF NOT it_knvp[] IS INITIAL.
    DELETE it_knvp FROM 1.
  ENDIF.
ENDIF.

DELETE ... FROM idx1

If you specify FROM, all the table rows from the table index idx1 onwards are included.

idx1 must be a data object with type i.

If the value of idx1 is less than or equal to 0, a runtime error occurs.

If the value is greater than the number of table rows, no rows are deleted.

best regards,

Thangesh

Former Member
0 Kudos

Small modification to the post above so that only duplicate records are deleted from internal table.

sort itab1 by kunn2.

itab2[] = itab1[].

sort itab2 by kunn2.

loop at itab1.

clear itab2.

clear count.

loop at itab2 whith kunn2 = itab1-kunn2.

count = count + 1.

if count > 1.

delete itab1 where kunn2 = itab2-kunn2.

exit.

endif.

endloop.

endloop.

itab1 will have the resultant.

Hope this helps.

Message was edited by: Imtiaz Ahmed

Former Member
0 Kudos

Hi

Try this:

SORT IT_KNVP BY KUNN2.

IT_KNVP_COPY[] = IT_KNVP[].

DELETE ADJACENT DUPLICATES FROM IT_KNVP COMPARING KUNN2.

LOOP AT IT_KNVP.

COUNT = 0.

LOOP IT_KNVP_COPY WHERE KUNN2 = IT_KNVP-KUNN2.

COUNT = COUNT + 1.

ENDLOOP.

IF COUNT > 1.

DELETE IT_KNVP.

ENDIF.

ENDLOOP.

Max

Former Member
0 Kudos

Hi Shejal,

Try this way:

data: it_knvp_prev like line of it_knvp.

data: begin of itab occurs 0,

kunn2 like knvp-kunn2,

end of itab.

sort it_knvp by kunn2.

loop at it_knvp.

if it_knvp_prev eq it_knvp.

move it_knvp-kunn2 to itab-kunn2.

append itab.

clear itab.

endif.

it_knvp_prev = it_knvp.

clear it_knvp.

endloop.

sort itab.

delete adjacent duplicates from itab.

delete it_knvp where kunn2 in itab.

Regards,

Vivek