Connecting Tech Pros Worldwide Help | Site Map

Eliminating Duplicate Rows In SQL SERVER 2000

jamesd0142's Avatar
Needs Regular Fix
 
Join Date: Sep 2007
Location: Wales
Posts: 467
#1   Oct 10 '07
My manager and I where looking at some complex code to eliminate duplicate records in a database table.

then it hit me how its done easily... so i thought i'd share it...

In English:
--------------------------------
-- Eliminating Duplicate rows --
--------------------------------
-- select all into a temp table.
-- truncate origional table.
-- select only unique rows back into origional table.

In Code sql server 2000:
--------------------------------
-- Eliminating Duplicate rows --
--------------------------------
Expand|Select|Wrap|Line Numbers
  1. -- select all into a temp table.
  2.  
  3. select * into temptable1 from <OrigionalTable>
  4. -- truncate origional table.
  5. delete <OrigionalTable>
  6.  
  7. -- select only unique rows back inot origional table.
  8. --this usues the selected columns to see if its unique
  9. insert into <OrigionalTable> select Distinct <columnName>  from TempTable1
  10.  
  11. --this uses all columnsto see if its unique
  12. insert into <OrigionalTable> select Distinct * from TempTable1
  13.  



Member
 
Join Date: Oct 2007
Posts: 71
#2   Nov 3 '07

re: Eliminating Duplicate Rows In SQL SERVER 2000


But wen im having multiple column duplication the above code doesnt works. In that situation wat can i do???

Regards,
Jeen
Newbie
 
Join Date: Aug 2007
Posts: 26
#3   Nov 3 '07

re: Eliminating Duplicate Rows In SQL SERVER 2000


Quote:

Originally Posted by jeenajos

But wen im having multiple column duplication the above code doesnt works. In that situation wat can i do???

Regards,
Jeen


DELETE FROM table_name where column_name in
(select column_name from table_name GROUP BY column_name HAVING COUNT(*)>1)
Newbie
 
Join Date: Nov 2007
Posts: 2
#4   Nov 9 '07

re: Eliminating Duplicate Rows In SQL SERVER 2000


This is what i've done .. creating an cursor and I've used it many times, hope it can help you.


--Declare all column names in table

DECLARE @Column1 decimal(9), @Column2 varchar(50), ect .....

DECLARE duplicate_cursor CURSOR FOR

-- select all columns in table bit you must have an count column
select Column1,Column2, count(*) Cnt, ect ...
from tblTable
group by Column1, Column2
Having count(*) > 1

OPEN duplicate_cursor

FETCH NEXT FROM duplicate_cursor
INTO @Column1, @Column2, ect ...

WHILE @@FETCH_STATUS = 0
BEGIN

SET @Cnt = @Cnt - 1

SET ROWCOUNT @Cnt

DELETE tblTable
WHERE @Column1 = Column1 AND @Column2 = Column1 AND ect ....


FETCH NEXT FROM duplicate_cursor
INTO @Column1, @Column2, ect ...
END

CLOSE duplicate_cursor
DEALLOCATE duplicate_cursor

-- dont forget to set rowcount to 0
SET ROWCOUNT 0
Newbie
 
Join Date: Nov 2007
Location: Bogotá
Posts: 4
#5   Nov 16 '07

re: Eliminating Duplicate Rows In SQL SERVER 2000


Most easy and fast.
Aply only for duplicate rows in all columns.
be carefully with the indexes and foreign key before to drop the original table.

In Code sql server 2000:

Expand|Select|Wrap|Line Numbers
  1. select distinct * into temp_table from <original_table>
  2. drop <OrigionalTable>
  3. sp_rename temp_table, original_table
  4.  
Newbie
 
Join Date: Dec 2007
Posts: 3
#6   Dec 25 '07

re: Eliminating Duplicate Rows In SQL SERVER 2000


Following website demonstrates easiest way to delete duplicate records from SQL Server (any version).

http://blog.sqlauthority.com/2007/03...-records-rows/

Regards,
Reply


Similar Microsoft SQL Server bytes