If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Delete duplicate records in table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-05, 10:33
expertcalling expertcalling is offline
Registered User
 
Join Date: Dec 2004
Posts: 1
Delete duplicate records in table

Hi All,

I have a table in SQL server 7 where all the records(rows) have been duplicated i.e. all rows have atleast 3 entries in the same table plus the table missed out the primary key. Now i wanted to delete all the duplicate records from the table at the same time one entry for the same (duplicate) row should remain in the database.
Can anyone help me out.

ExpertCalling
Reply With Quote
  #2 (permalink)  
Old 01-03-05, 12:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what does "plus the table missed out the primary key" mean?

how many columns are in this table? can you show some sample rows?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-03-05, 17:17
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool


You can try this:

1) Rename the table with the duplicates.
2) Create a new empty table with primary key enabled.
3) Insert 'DISTINCT' from old table -- or:

Code:
INSERT INTO New_Table
SELECT * FROM Old_Table O
 WHERE NOT EXISTS (
       SELECT 1 FROM New_Table N
        WHERE N.Primary_Key = O.Primary_Key)
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On