MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
Deleting duplicate rows when there is no primary key on a SQL Server database table -

in Search

Deleting duplicate rows when there is no primary key on a SQL Server database table

Last post 05-18-2008 11:57 PM by balakumar.sk. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 11-10-2006 12:00 AM

    Deleting duplicate rows when there is no primary key on a SQL Server database table

    This post is related to this tip: Deleting duplicate rows when there is no primary key on a SQL Server database table

    http://www.mssqltips.com/tip.asp?tip=1103

  • 03-18-2008 2:29 AM In reply to

    • Bals
    • Top 50 Contributor
    • Joined on 03-18-2008
    • Madurai
    • Posts 3

    Re: Deleting duplicate rows when there is no primary key on a SQL Server database table

    The corelated query in TOP clause is not working .. Basharat Bhat has given only for single dup entry

    Regards
    BalaMurugan M R
    SQL Programmer
    Honeywell Technology Solutions
    India
  • 04-04-2008 7:38 AM In reply to

    Re: Deleting duplicate rows when there is no primary key on a SQL Server database table

    Bals,

    Can you please post the code you are having an issue with?

    Thank you,
    The MSSQLTips.com Team

    Filed under: ,
  • 05-06-2008 1:16 PM In reply to

    • Gadi35
    • Top 200 Contributor
    • Joined on 05-06-2008
    • Posts 1

    Re: Deleting duplicate rows when there is no primary key on a SQL Server database table

    There is another very good way to do this, in SQL Server 2005, if you do not have the key set up, but want to effectively use a column or more as your primary key.

     ;with DelDup as (select row_number() over (partition by
    FirstName, LastName
    order by FirstName, LastName) as RowNofrom duplicateTest)
    Delete from DelDup where RowNo> 1

    --alternate--

     ;with DelDup as (select row_number() over (partition by
    ID
    order by ID) as RowNofrom duplicateTest)
    Delete from DelDup where RowNo> 1

    You can easily adjust this to use one column or as many as you wish, as long as you update the column(s) after the partition by and after the order by.

  • 05-09-2008 2:29 PM In reply to

    Re: Deleting duplicate rows when there is no primary key on a SQL Server database table

    Thank you both for the feedback.

    Thank you,
    The MSSQLTips.com Team

  • 05-15-2008 7:31 AM In reply to

    Re: Deleting duplicate rows when there is no primary key on a SQL Server database table

    Hi Friend Pls test the following one, dis s a very simple method

    Delete Top (Select Count(*)-1 From EmpTest Where EmpId=A.EmpId)
    From EmpTest As A

     

     

  • 05-18-2008 11:57 PM In reply to

    Re: Deleting duplicate rows when there is no primary key on a SQL Server database table

    It didnt work Prakash,

    gave me the follwoing error

    Msg 156, Level 15, State 1, Line 2

    Incorrect syntax near the keyword 'As'."

     

Page 1 of 1 (7 items)