Delete duplicate records in table

  • I have a simple table that had 2 records accidentally added where every field matches between the 2 records. The problem is deleting the records. Sql server will not let me remove the records.

    There are no indexes or keys attached to this table yet.

    The error message reads:

    The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(2 rows).

    How can I remove these 2 records?

  • Give this a try:

    SET ROWCOUNT 1

    DELETE from #tmp where (add all the columns you need to identify the record you want to delete)

    SET ROWCOUNT 0

    The rowcount will limit the delete to one of the two rows.

    Chad

  • Either remove all of them, then re-insert

    The SET ROWCOUNT 1 is a neat idea I didn't think of

    I was gonna suggest adding an ID identity field, that way you have a UNIQUE key for each row

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005

Viewing 3 posts - 1 through 2 (of 2 total)

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