Get rid of duplicate records in a table using SQL Server 2005

  • Hi All,

    Could anyone assist on this? I have a table that contains duplicate records and would like to get rid of them (duplicates) as we need only unique records within a specified table in SQL Server 2005. We created a composite index in a new table and tried to append all records from the table that contains duplicates to eliminate them on the new table, but not even a records has been appended on that new table, why? It was always working fine when using SQL Server 2000. Can anyone assist on this please? Atang.

  • This topic has been covered many times. Search "Delete Duplicate", and get pages of threads such as these with different methods.

    http://www.sqlservercentral.com/Forums/Topic652644-338-1.aspx

    http://www.sqlservercentral.com/Forums/Topic825096-338-1.aspx

    http://www.sqlservercentral.com/Forums/Topic403032-338-1.aspx

  • Hi, Thanks for the respond.

    My situation here is different to what you've refered me to. Using SQL Server 2000, we used to have 1 central table that we all query from on a daily basis and at the same time append new records to it. We set indexes into that table (tblModels) for these 2 fields (Modelcode and Modeldescription) to ignore duplicates. We do not want it to have duplicates records only unique records must be there. MS SQL Server 2000 used to insert only unique records but with MS SQL Server 2005, when ever new data contains duplicates records within the whole transaction rolls back and nothing gets inserted there which will be a problem for us.

    Thanks.

  • One way this can be achieved is to put the rows to insert into a temporary table and then select from the temporary table only the rows that do not exist in the table where you want to insert the data finally.

    Step 1: Select all rows to insert into myTblModels

    Step 2: Select from myTblModels where row does not exists in final table.Insert these rows into final table:

    Insert into tblModels(column1,column2,....)

    Select *

    From myTblModels t

    Where Not Exists(Select 1 From tblModels f Where t.key1 = f.Key1 and t.Key2 = f.Key2.....)

  • Hi There,

    Yes i do agree that this question has been revolving round and round but i would like to get help as well. I used to work on MS SQL Server 2000 and now using MS SQL Server 2005, I used to have 1 central table(tblModels) that has unique key on both "Manuf" and "ModelDes" that allow only unique records in and we're quering from that table on a daily basis. Now i have created the same table with same records and index key on both fields using MS SQL Server 2005.But when ever i try to append records that we receive daily into tblModels, if there are duplicates contained in that records, the whole transaction is rolled back and nothing get appended. How can i get rid of these?

    Example of Syntax:

    Insert into Server.DBName.dbo.tblModels (InvalidModelDescription,ManufacturerID)

    Select distinct RawModelDes,Manuf

    From tblPrepTemplate_SMS

    WHERE RawModelDes NOT IN

    (SELECT InvalidModelDescription

    FROM "Server".DBName.dbo.tblModels where ManufacturerID IS NULL)

    Select Count(*) as Count,InvalidModelDescription,ManufacturerID

    From dbo.lutModels

    Group by InvalidModelDescription,ManufacturerID

    Having Count(*)>1

    Order By InvalidModelDescription

    Error Msg " it complains about duplicate records on index key fields"

  • First, there isn't a difference between the versions of SQL2000 or SQL2005 when it comes to PK's, or Unique index. The difference is how the objects were created in SQL2005.

    From what I understand, now when you insert a record you are getting a "cannot insert duplicate record in object xxx" error and a rollback occurs, which is the correct behavior when you have a unique index, or PK.

    The question is why are you inserting a duplicate record?

    Are you trying to "cleanup" the duplicate records? If so, the above links will help with that.

    Would you want to update the record instead, or are you always inserting?

    What about logic such as:

    If exists (select 1 from tablex where uniquekey = @uniquekey)

    begin

    update tablex set ....

    end

    else

    begin

    INSERT into tablex ...

    end

  • Hey, thank u for the respond.

    Yes we usually append records into that table(tblModels) on a daily basis as we do receive more than 50 000 records and duplicates records are included there,we want that to allow only unique records.

    That table is actually using by different teams including our local call centers.What i need is the solution to maintain this table by not including duplicate records in order for other team member who is responsible for correcting new rocords according to our business rules. Because if we have duplicates within that table,size will grow dramatically, more work will be assigned to him to correct even the same records we've received before and which will delay our process and timing.

    Please assist.

  • Is this a bulk insert operation, or just inserts as transactions occur?

    If it's just "inserts" as a transaction occurs in the application you may want to follow logic such as.

    1. I'm assuming that duplicate records currently don't exist on the table because of the key/index. If they do then you'll want to remove the duplicates by placing them in a table (DuplicateModel) using the logic that was outlined above "having count(*) > 1" This table has the same columns as the Model table but with a few additional columns (AuditDate, AuditUser, ApplicationName) or whatever will be useful in troubleshooting.

    2. Place the code in a TRY CATCH block, or use the IF EXISTS (SELECT 1 from...) logic.

    If record exists then you can return a message to the application saying "Record already exists for this customer", instead of performing the UPDATE that I mentioned above.

    OR

    If record exits you could always INSERT it into the table (DuplicateModel) mentioned in step 1. This way you'll know of the duplicate record and can record the datetime of the transaction as well as the user who inserted the record.

    I think this should get you going in the correct direction.

  • Hi there,

    Thanks again for the respond. Herewith is my example of a simple syntax below which i tried to use but nothing gets appended.

    Insert into tblModels (Column1,Column2,Column3,Column4,...)

    Select Distinct Column1,Column2,Column3,Column4,....

    From tblReceivedData

    Where Column1 not in( Select Column1 From tblModels where tblModels .Column2= tblReceivedData.Column2)

    Remember clustered index has been set onto both column1 & Column2. I just need to get this work but transaction get rolled back when it reached duplicate value.

    Regards,

    Atang

  • How about:

    Insert into tblModels (Column1,Column2,Column3,Column4,...)

    SELECT

    n.COL1, n.COL2, n.COL3, n.COL4, ...

    FROM tblReceivedData n

    where

    NOT EXISTS

    (SELECT o.col1, o.col2 from tblModels o

    where n.col1 = o.col1

    and n.col2 = o.col2)

  • It was stated before - there are volumes written on how to remove duplicate records. Your case is not unique.

    If you feel that these ways do not work for you, then you could try to create a primary (unique) key on the table. The table would have to be empty. Set the IGNORE_DUP_KEY = ON. This would allow the records to be inserted, and if a duplicate record was attempted to be inserted it would give you an error and throw the record out. The process though would continue. This may be what you need for the future as well since you may have the problem on a daily basis from the way it sounds.

    Steve Jimmo
    Sr DBA
    “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply