Application Development and Automation 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: 
Read only

internal table Comparison

Former Member
0 Likes
877

Hi,

I hav 2 internal tables with some values . I want to compare the 2 tables and elimate

duplicate entries.Wht logic i hav to use in looping ?

Rgds,

SAPUser100

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
856

Which internal table do you want to remove the duplicates from, the first, or second.

If it is ok to combined the internal tables into one. Then I would suggest this.

* Add the contents of itab2 to itab1.
Loop at itab2.

clear itab1.
move itab2 to itab1.
append itab1.

Endloop.

* Now get rid of the duplicate records.
delete adjacent duplicates from itab1.

Regards,

Rich Heilman

7 REPLIES 7
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
857

Which internal table do you want to remove the duplicates from, the first, or second.

If it is ok to combined the internal tables into one. Then I would suggest this.

* Add the contents of itab2 to itab1.
Loop at itab2.

clear itab1.
move itab2 to itab1.
append itab1.

Endloop.

* Now get rid of the duplicate records.
delete adjacent duplicates from itab1.

Regards,

Rich Heilman

Read only

0 Likes
856

Hi,

just a small correction... sort itab1 & then use the delete adjacent duplicates statement.

On the other hand, if you want to delete an entry in itab1 if it exists in itab2.. then it depends on the structure of the itabs. You can loop at one itab and read the other itab with key and delete the entry if

sy-subrc eq 0.

Regards,

Suresh Datti

Message was edited by: Suresh Datti

Read only

0 Likes
856

Yes, of course, I always forget that. Thanks Suresh.



* Add the contents of itab2 to itab1.
Loop at itab2.
 
clear itab1.
move itab2 to itab1.
append itab1.
 
Endloop.


* Sort it
Sort itab1 ascending.

* Now get rid of the duplicate records.
delete adjacent duplicates from itab1

Regards,

Rich Heilman

Read only

0 Likes
856

Sam's got the right idea if you want to delete the "duplicates" out of itab2. If it is the other way around, then you would do the same thing swapping the itab names. If you want to do get rid of the duplicates in both itabs and keep both itabs you will have to do it twice.

LOOP AT itab1.
  READ TABLE itab2 WITH KEY val = itab1-val.
  IF sy-subrc = 0.
    DELETE itab2 INDEX sy-tabix.
  ENDIF.
  CLEAR: itab1,
         itab2.
ENDLOOP.


LOOP AT itab2.
  READ TABLE itab1 WITH KEY val = itab2-val.
  IF sy-subrc = 0.
    DELETE itab1 INDEX sy-tabix.
  ENDIF.
  CLEAR: itab1,
         itab2.
ENDLOOP.

Regards,

Rich Heilman

Read only

0 Likes
856

you can also give a field in the internal table

you want for your comparison:

delete adjacent duplicates from itab1 comparing table_line.

Regards,

Marc

Read only

Former Member
0 Likes
856

you alsco can directly copy the data of one table to another:

<i>append lines of it1 [from nf] [to nt] to it2.</i>

then just do delete duplicate entries as above.

Read only

Former Member
0 Likes
856

Is this what you want?


LOOP AT itab1.
  READ TABLE itab2 WITH KEY val = itab1-val.
  IF sy-subrc = 0.
    DELETE itab2 INDEX sy-tabix.
  ENDIF.
  CLEAR: itab1,
         itab2.
ENDLOOP.