Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How do i delete duplicate records ?

Author  Topic 

missMac
Posting Yak Master

124 Posts

Posted - 2008-12-04 : 14:51:10
Hi guys,
1. How do i delete duplicate records ?

Which is better during an insert.

(a) To run an insert with a sub query to search if not exists
(b) Run an insert and then delete record counts having more than 1 entry ?

insert into dbo.PhoneNumbers (Username, Number, NickName, Group_Name )
select distinct @user, recepient, recepient, @mergeID from filter f
where Username = @user and BatchNumber = @recipient
and not exists(select 1 from dbo.Phonenumbers where number = f.number)

thanks
MM



RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-12-04 : 15:33:10
1. Insert the record to a temp table using max on each field (and a group by obviously), delete both rows and reinsert the row from the temp table.

Option a is the best method and has the least overhead and maintenece cost.
Go to Top of Page

missMac
Posting Yak Master

124 Posts

Posted - 2008-12-05 : 14:23:28
thanks Bruv
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-05 : 23:39:05
quote:
Originally posted by missMac

Hi guys,
1. How do i delete duplicate records ?

Which is better during an insert.

(a) To run an insert with a sub query to search if not exists
(b) Run an insert and then delete record counts having more than 1 entry ?

insert into dbo.PhoneNumbers (Username, Number, NickName, Group_Name )
select distinct @user, recepient, recepient, @mergeID from filter f
where Username = @user and BatchNumber = @recipient
and not exists(select 1 from dbo.Phonenumbers where number = f.number)

thanks
MM






what happens if select query itself contain duplicate records in first case?
i think if select query contain duplicate values, then (b) is solution.
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-12-06 : 08:19:41
No, just change the select to not have duplicates. If the select has duplicates, you are doing something wrong.
Go to Top of Page
   

- Advertisement -