Delete Duplicate record from SQL Server2000

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajeshkrsingh
    New Member
    • Oct 2007
    • 2

    Delete Duplicate record from SQL Server2000

    Hi friends,

    Step1-
    create table duplicate
    (
    intId int, varName varchar(50)
    )
    insert into duplicate(intId ,varName) values(1,'rajes h')
    insert into duplicate(intId ,varName) values(2,'raj12 ')
    insert into duplicate(intId ,varName) values(1,'rajes h')
    insert into duplicate(intId ,varName) values(2,'raj12 ')
    insert into duplicate(intId ,varName) values(3,'raj12 ')

    Step2-
    ---Create Duplicate table
    create table #dupTbl
    (
    intId int, varName varchar(50)
    )
    insert into #dupTbl
    select intId,varName from duplicate group by intId,varName having count(*) > 1

    Step3-

    delete from duplicate where intId in (select intId from #dupTbl)

    Step4-

    insert into duplicate
    select intId,varName from #dupTbl

    Finished....... ........
    You can checked your table...
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    Additionally look at this post... i believe its a simpler way and works well for me!

    Comment

    • Munawar
      New Member
      • May 2007
      • 12

      #3
      hi,

      The above link has a good way to accoplish the task but here is another possibility but would work if there is some id column;

      Delete all duplicate records ..
      select distinct will bring all the uniquie records and rest would the duplicate and will be deleted.

      Code:
      Delete * from YourTable where ID <> IN ( SELECT Distinct IDColumn from YourTable)
      Thanks,
      Munawar

      Comment

      • pinaldave
        New Member
        • Dec 2007
        • 3

        #4
        Easiest Way to Delete Duplicate Rows

        Following website demonstrates easiest way to delete duplicate records from SQL Server (any version).

        Following code is useful to delete duplicate records. The table must have identity column, which will be used to identify the duplicate records. Table in


        Regards,

        Comment

        Working...