| | |
how to delete duplicate record in a table by using SQL query
Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved |
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:
so everytime you insert any row, it will increase 1. so there will a unique number for every row 
Regards!
while creating table just add an extra attribute using identity keyword:
MS SQL Syntax (Toggle Plain Text)
attributename int identity(1,1)

Regards!
Last edited by puneetkay; Mar 6th, 2008 at 7:36 am.
mssql Syntax (Toggle Plain Text)
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 *
So, Please do something before post your thread.
* PM Asking will be ignored *
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
Other Threads in the MS SQL Forum
- Previous Thread: Changing the datatype of a Column
- Next Thread: syntax query for backup database for one month in sql server 2000
| Thread Tools | Search this Thread |






