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

duplicate entries in internal table

Former Member
11,541

hi experts,

how to find the dulicate falues in in internal table?
i have an internal table with 12 fields.

i would like to check for duplicate values in the first field on each record. an error message should be sent for each dupicate entries found without deleting the table entries.

could anyone help me?

hi experts,

how to find the dulicate falues in in internal table?
i have an internal table with 12 fields.

i would like to check for duplicate values in the first field on each record. an error message should be sent for each dupicate entries found without deleting the table entries.

could anyone help me?

16 REPLIES 16
Read only

venkat_aileni
Contributor
0 Likes
8,595

Hi-

What you can do is: Move your final internal table values into an temporary table. And delete duplicates in temporary table comparing your first field name. Then by using DESCRIBE statement find the number of entries in both the table if they are equal then no duplicates else populate an error message.

-Venkat

Read only

asim_isik
Active Participant
0 Likes
8,595

hi,

define 2 structure for your itab.

sort itab.

loop at itab into gs_itab1

if gs_itab1 eq gs_itab2.

message 'Table has duplicate entries' type 'W'.

endif.

move gs_itab1 to gs_itab2.

endloop.

Read only

Former Member
0 Likes
8,595

he only want compare the first field, so in your code,

if gs_itab1-firstfield eq gs_itab2-firstfield.

this will be better, i think.  thanks.

Archer.

Read only

Former Member
0 Likes
8,595

Hi Sebin,

First sort your table on the field which has duplicates then loop at the internal table and compare with previous entry of table if sy-tabix is greater than 1. If they match then it means duplicates exist. Throw and error message then for each duplicate you find this way.

Regards,

Sheetal.

Read only

Former Member
0 Likes
8,595

Hi Sebin,

UI ca do this check.

suppose u have internal table ITAB which contains ur data.

take same type of TWO more table ITAB1,ITAB2.

DATA : LV_LINES TYPE SYTABIX.

ITAB1[] = ITAB[]." KEEP ALL DATA

SORT ITAB1 BY FIRST FIELD.

DELETE ADJACENT DUPLICATE FROM ITAB1 COMPARING FIRST_FIELD.

LOOP AT ITAB1 INTO WTAB1.

ITAB2[] = ITAB[].

DELETE ITAB2 WHERE FIRST_FIELD NE WTAB1-FIRST_FIELD.

DESCRIBE ITAB2 LINES LV_LINES.

IF LV_LINES GT 1.

* STORE UR ERROR

ENDIF.

CLEAR LV_LINES.

FREE : ITAB2[].

ENDLOOP.

It will sort out ur requirement.

Thanks

Tarak

Read only

Former Member
0 Likes
8,595

Hi

Please find below the steps

itab_temp = itab_original.

Loop at itab_orginal into wa.

read itab_temp  transporting no fields with key field_1 = wa_field_1.

      if sy-subrc = 0.

       // here add your code for generating error message or fill error message in some internal table so as to display to user later on

     endif.

endloop.

Hope this give you some idea!!

thanks

Read only

Former Member
0 Likes
8,595

Hi,

Please find a file attached.You will find the source code.

To check the code please debug and check the internal table e_return,

which contains all the rows which are repeated. This will help.

Regards

Read only

Former Member
0 Likes
8,595

Hi Sebin,

Please find the below steps, I believe it will solve Ur issue:

SORT it_tab by first_field.

READ TABLE it_tab ASSIGNING <fs> index 1.

   IF sy-subrc = 0.

     IF sy-tabix < lines( it_tab ) .

       READ TABLE it_tab ASSIGNING <fs_temp> INDEX ( sy-tabix + 1 ).

IF sy-subrc = 0 AND <fs_temp> IS ASSIGNED AND <fs_ekpo_temp> = <fs>.

    

"Add Ur code   

    

       ENDIF.

     ENDIF.

ENDIF.

Read only

praveenboss
Participant
0 Likes
8,595

Hy Sebin,

    Dear You Can Solve Your Mobile,

   

    You Can Move Your All Record to another table which same structure,

    and then from new table you sort that based on that field,

    and then delete duplicates from table based on sorted field,

    if sy-subrc = 0.

    then message

    else.

    original table does not contain duplicates on that field

    endif.

thanks,

PRAVEEN

Read only

Former Member
0 Likes
8,595

Hi,

You can use AT NEW statement and check for value of sy-tabix .

DATA TABIX TYPE SY-TABIX.

LOOP AT ITAB INTO WA_ITAB.

          ADD 1 TO TABIX.                              "FIELDNAME is name of field to check for duplicates.

          IF SY-TABIX <> TABIX.

               MESSAGE 'DUPLICATE' TYPE 'I'.

          ENDIF.

          TABIX = SY-TABIX.

     ENDAT.

ENDLOOP.

Read only

former_member598305
Participant
0 Likes
8,595

I dont know how much it is useful but I have an example try with that it may give some clarity .

Find the below.

TYPES: BEGIN OF ty,
   name type string,
   num1 type i,
   num2 type i,
   END OF ty.

data: it type TABLE OF ty,
       wa type ty.
data: it2 type TABLE OF ty,
       wa2 type ty.
data: it3 type TABLE OF ty,
       wa3 type ty.
wa-name  = 'sudhir'.
   wa-num1 = 1.
   wa-num2 = 2.
   append wa to it.

     wa-name = 'sebin'.
     wa-num1 = 12.
   wa-num2 = 22.
   append wa to it.

   wa-name  = 'sudhir'.
   wa-num1 = 3.
   wa-num2 = 4.
   append wa to it.

    wa-name  = 'sudhir'.
   wa-num1 = 55.
   wa-num2 = 66.
   append wa to it.

     wa-name = 'sebin'.
     wa-num1 = 99.
   wa-num2 = 77.
   append wa to it.

   sort it by name.
*  loop at it into wa.
*    write:/ wa-name, wa-num1, wa-num2.
*    endloop.

it2[] = it[].

sort it2 by name.
BREAK-POINT.
data temp type string.
DATA: TAB TYPE I.
loop at it into wa.
   IF WA-NAME NE TEMP.
   loop at it2 into wa2 where name = wA-name.
   TAB TAB + 1.
    IF TAB NE 1.
     APPEND WA2 TO IT3.
    ENDIF.
    TEMP = WA2-NAME.
   endloop.
   CLEAR TAB.
   ENDIF.
endloop.



It3 will have all duplicate values from the original table pls do debug and execute and if u r scenario is not this then pls excuse me

Read only

0 Likes
8,595

Hi Sebin  ,

I would like to give you simple method  .

just add one number field  in your internal table  with default value 1.

perform  collect on sorted table . it will add number field  if it is duplicate or more than one  .

thus by checking that column whether it is more than one you can say it is duplicate provided that

all other fields are  character field other than one number field  .

regards

Deepak .

Read only

0 Likes
8,595

hi sebin,

Check this code , hope this will help u .

types: begin of ty_tmp,

      fld1 type i,

      fld2 type c,

      end of ty_tmp.

data: it_tmp type standard table of ty_tmp,

      wa_tmp1 type ty_tmp,

      wa_tmp type ty_tmp.

data:v_idx type sy-tabix.

sort it_tmp by fld1.

loop at it_tmp into wa_tmp.

v_idx = sy-tabix + 1.

read table it_tmp into wa_tmp1 index v_idx.

if sy-subrc is initial and wa_tmp1-fld1 eq wa_tmp-fld1.

message 'Duplicate entry' type 'E'.

endif.

endloop.

thanks

regards

Shravan

Read only

Former Member
0 Likes
8,595

Dear Experts,

Thank you very much for your support and suggestions.
i completed the task, again thanks for your kind help.

what i did is,

---------------------------------------

Data:
itab- internal tabl which has got data.

wa_itab-

itab1- temp table of same type itab.
wa_itab1

----------------------------------------

loop itab into wa_itab

read table itab1 into wa_itab1 with key (give fields)

if sy-subrc eq 0

<duplicate record found>

endif.

else

<remaining statements/ operations with itab.>

endif.
append wa_itab to itab1.
endloop.


thank you very much for your kind help and support experts


Read only

0 Likes
8,595

Hi,

In your case you can use the addition of "TRANSPORTING NO FIELDS"

instead of "read table itab1 into wa_itab1 with key (give fields)".

Regards.

Read only

0 Likes
8,595

thanks Eitan