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: 

How to check duplicate entries in internal table??

Former Member
0 Kudos

Dear Friends,

How to check duplicate entries in internal table??

Exp: In my internal table if I am having the same records more then ones then I need to print the error message, here I am using steploop for selecting the values from screen, and the values are coming into my internal table if user enter the same value more then ones I need to print the error message.

Thanks,

Sridhar

9 REPLIES 9

Former Member

after populating internal table move it to second internal table...

itab1[] = itab[].

describe table itab lines var1.

delete adjacent duplicates from itab1 comparing all fields.

describe table itab1 lines var2.

if var1<>var2.

*---there are dupliacte records

endif.

kesavadas_thekkillath
Active Contributor
0 Kudos

consider u r first field of itab consists of duplicate values.

try this pseudocode.

data:flag type c.

sort itab by field ascending.

read table itab index 1.

move field value to varialble.

loop at itab from 2.

if itab-fieldvalue <> variable.

variable = itab-fieldvalue.

continue.

else.

flag = 'X'.

exit.

endif.

endloop

if flag = 'X'.

duplicate found.

endif.

reward if usefull

0 Kudos

Thanks Team...

Solved the problem.

Former Member
0 Kudos

Since you are working with step loops, you cannot sort the data otherwise the sequence in which user has entered the data will be disturbed.

Let's say your current table itab is having all the current screen records. Your current record needs to be available in your workarea.

itab_temp[] = itab[].

delete the record from ITAB_temp index sy-stepl. ---> This will delete the current record against which you are checking for duplicates from the temporary table.

read itab_temp for the matching entry in the work area. if any record is found, it will imply that your step loop is having a duplicate entry.

0 Kudos

Thanks for the Answer its usefull me.

do you have any idea why my steploop as greyed out if I print the error message??

I mean to say If there are duplicates I have given a error message but at the same same the remaining fields in my screen(steploop fields) are greyed out. I want to bring them input enable mode. How to do??

Thanks,

Sridhar

Former Member
0 Kudos

Hi,

declare one more internal table IT_ERR same as ITAB

IT_ERR[] = ITBA[].

DELETE ADJACENT DUPLICATES FROM IT_ERR.

if sy-subrc = 0.

Message E123(ZK_MSGCLS).

endif.

Try this,

KC

Former Member
0 Kudos

Hi,

After storing the data into internal table say ITAb, move the data into another internal table.

t_dup[] = itab[].

LOOP AT itab.

count1 = count1 + 1.

itab-count1 = count1.

MODIFY itab.

ENDLOOP.

LOOP AT t_dup.

count2 = count2 + 1.

t_dup-count2 = count2.

MODIFY t_dup.

ENDLOOP.

DELETE ADJACENT DUPLICATES FROM itab.

LOOP AT t_dup.

record_dup = 'N'.

READ TABLE itab WITH KEY count1 = t_dup-count2.

IF sy-subrc = 0.

record_dup = 'Y'.

ENDIF.

IF record_dup NE 'Y'.

t_dup-message = 'DUPLICATE ENTRY'.

t_dup-flag = 1.

MODIFY t_dup.

ENDIF.

ENDLOOP.

Use this sample code.

Reward pts if it is helpfull.

Regards

Srimanta

Former Member
0 Kudos

Thanks for the Answer its usefull me.

do you have any idea why my steploop as greyed out if I print the error message??

I mean to say If there are duplicates I have given a error message but at the same same the remaining fields in my screen(steploop fields) are greyed out. I want to bring them input enable mode. How to do??

Thanks,

Sridhar

former_member197578
Participant
0 Kudos

Hi ,

You may use following logic to delete duplicate entries from an internal table and show in the output the specific entry which is duplicate as an error:

Let say ITAB1[ ] has all your data. Sort it based on key and then assign it to another temporary table ITAB2[ ].

i.e. ITAB1[ ] = ITAB2[ ].

Now, DELETE ADJACENT DUPLICATES from table ITAB2[ ] comparing the key. Now ITAB2[ ] has all but unique entries from ITAB1 [ ] while your original table ITAB1[ ] has all the entries (Including duplicate ones).

Now, LOOP AT ITAB1[ ] INTO DATA(LWA_ITAB1). (Loop at original table)

*** "Start of Comment" Inside this LOOP, READ ITAB2[ ] and delete it for that particular entry If SY-SUBRC = 0 (If read is successful). This deletion would remove that particular unique entry from ITAB2[ ] and now if it's READ again for the same entry it should throw an error (Sy-subrc <> 0) and at the same time it would also mean that it's being read for a duplicate entry from ITAB1[ ].

So now, if next time in ITAB1[ ]LOOP sy-subrc <> 0, this would mean that the current entry is duplicate as it was already found once and hence deleted. *** End of Comment

** PSEUDOCODE

READ TABLE ITAB2[ ] INTO DATA(lwa_itab2) WITH KEY KEY1 = .LWA_ITAB1-KEY1.

IF SY-SUBRC = 0.

DELETE ITAB2[ ] WHERE KEY1 = LWA_ITAB1-KEY1.

ELSE.

Throw an error or append error to return table saying KEY1 has duplicate entries.

ENDIF.

ENDLOOP.