Home Forums SQL Server 2005 Administering Get rid of duplicate records in a table using SQL Server 2005 RE: Get rid of duplicate records in a table using SQL Server 2005

  • 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.