| Author |
Topic |
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-03-07 : 06:28:13
|
| hi,create TABLE #t ( id int,names varchar(50))INSERT INTO #t VALUES(1,'master')INSERT INTO #t VALUES(2,'tempdb')INSERT INTO #t VALUES(2,'tempdb')INSERT INTO #t VALUES(3,'model')INSERT INTO #t VALUES(3,'model')INSERT INTO #t VALUES(3,'model')INSERT INTO #t VALUES(1,'master')SELECT * FROM #tIam using sql server 2005. I want to delete the duplicate records from the table.Please specify a method which is very good at performance.Thanks in Advance.......... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-07 : 06:31:44
|
| [code]DELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY id,names ORDER BY id) RowNo,id,namesFROM #t)tWHERE t.RowNo<>1[/code] |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-03-07 : 07:12:57
|
| Be sure that you define a primary key on this table, and for *all* tables in your databases.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-03-07 : 07:31:18
|
| Thanks to all for their replies and suggestions... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
raky
Aged Yak Warrior
767 Posts |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-03-08 : 01:07:47
|
quote: Originally posted by jsmith8858 raky -- you are going to define primary keys on all of your tables now, right?- Jeffhttp://weblogs.sqlteam.com/JeffS
surely... |
 |
|
|
amirmuthu
Starting Member
12 Posts |
Posted - 2008-03-10 : 06:51:15
|
| --------------Delete Duplicate Record---------------SET ROWCOUNT 1DELETE empFROM emp aWHERE (SELECT COUNT(*) FROM emp b WHERE b.cid = a.cid AND b.cid = a.cid) > 1WHILE @@rowcount > 0DELETE empFROM emp aWHERE (SELECT COUNT(*) FROM emp b WHERE b.cid = a.cid AND b.cid= a.cid) > 1SET ROWCOUNT 0 |
 |
|
|
mfdarvesh
Starting Member
10 Posts |
Posted - 2008-08-26 : 08:42:18
|
| amirmuthu, Thanks, it is very useful cursor, applied and worked correctly |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-26 : 08:46:03
|
quote: Originally posted by amirmuthu --------------Delete Duplicate Record---------------SET ROWCOUNT 1DELETE empFROM emp aWHERE (SELECT COUNT(*) FROM emp b WHERE b.cid = a.cid AND b.cid = a.cid) > 1WHILE @@rowcount > 0DELETE empFROM emp aWHERE (SELECT COUNT(*) FROM emp b WHERE b.cid = a.cid AND b.cid= a.cid) > 1SET ROWCOUNT 0
Must be the most slowest method ever? Why did you think this is preferred over other methods?MadhivananFailing to plan is Planning to fail |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-08-26 : 09:51:29
|
I assume you really want to keep just one copy of each row.set nocount oncreate TABLE #t ( id int,names varchar(50))INSERT INTO #t VALUES(1,'master')INSERT INTO #t VALUES(2,'tempdb')INSERT INTO #t VALUES(2,'tempdb')INSERT INTO #t VALUES(3,'model')INSERT INTO #t VALUES(3,'model')INSERT INTO #t VALUES(3,'model')INSERT INTO #t VALUES(1,'master')INSERT INTO #t VALUES(5,'msdb')set nocount offprint 'Find rows with dupes'select id ,namesinto #t2from #tgroup by id ,nameshaving count(*) > 1order by id ,namesprint 'Delete rows with dups'delete from #tfrom #t join #t2 on #t.id = #t2.id and #t.names = #t2.namesprint 'Re-Insert rows'insert into #tselect * from #t2 order by id,namesprint 'Final #t'SELECT * FROM #tgoif object_id('tempdb..#t','u') is not null drop table #tif object_id('tempdb..#t2','u') is not null drop table #t2Results:Find rows with dupes(3 row(s) affected)Delete rows with dups(7 row(s) affected)Re-Insert rows(3 row(s) affected)Final #tid names ----------- -------------------------------------------------- 1 master2 tempdb3 model5 msdb(4 row(s) affected)CODO ERGO SUM |
 |
|
|
|