how to delete duplicate record in a table by using SQL query

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: how to delete duplicate record in a table by using SQL query

 
0
  #11
Mar 6th, 2008
Well guys.. im new for MS SQL.. i have a solution & it worked perfectly with me.

while creating table just add an extra attribute using identity keyword:
  1. attributename int identity(1,1)
so everytime you insert any row, it will increase 1. so there will a unique number for every row

Regards!
Last edited by puneetkay; Mar 6th, 2008 at 7:36 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: how to delete duplicate record in a table by using SQL query

 
0
  #12
Mar 6th, 2008
  1. DELETE FROM emp e WHERE rowid>(SELECT MIN(ROWID) FROM emp WHERE e.empno=empno)
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: space1000 is an unknown quantity at this point 
Solved Threads: 1
space1000 space1000 is offline Offline
Newbie Poster

Re: how to delete duplicate record in a table by using SQL query

 
0
  #13
Mar 7th, 2008
Just solve the thing with a cursor

like
declare del_dups cursor
for
select * from <table_name>
group by column_name
having count(*) > 1

(column_name where the duplicates are)

open del_dups

fetch next from del_dups into ......

while @@fetch_status <> 0
begin
delete from table_name where current of cursor
fetch next from del_dups into ....
end

close del_dups
deallocate del_dups

For the update you should change te delete statement to

update table_name set ... = .... where current of cursor
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: edgarkuhimbisa is an unknown quantity at this point 
Solved Threads: 0
edgarkuhimbisa edgarkuhimbisa is offline Offline
Newbie Poster
 
0
  #14
Oct 30th, 2009
hey im workin out smthin hre will get back to u. i seem to be havin an idea
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC