SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get rid of duplicate records in a table using SQL Server 2005


Get rid of duplicate records in a table using SQL Server 2005

Author
Message
Atang Mogwera
Atang Mogwera
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 12
Hi All,
Could anyone assist on this? I have a table that contains duplicate records and would like to get rid of them (duplicates) as we need only unique records within a specified table in SQL Server 2005. We created a composite index in a new table and tried to append all records from the table that contains duplicates to eliminate them on the new table, but not even a records has been appended on that new table, why? It was always working fine when using SQL Server 2000. Can anyone assist on this please? Atang.
homebrew01
homebrew01
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12344 Visits: 9222
This topic has been covered many times. Search "Delete Duplicate", and get pages of threads such as these with different methods.

http://www.sqlservercentral.com/Forums/Topic652644-338-1.aspx

http://www.sqlservercentral.com/Forums/Topic825096-338-1.aspx

http://www.sqlservercentral.com/Forums/Topic403032-338-1.aspx



Atang Mogwera
Atang Mogwera
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 12
Hi, Thanks for the respond.
My situation here is different to what you've refered me to. Using SQL Server 2000, we used to have 1 central table that we all query from on a daily basis and at the same time append new records to it. We set indexes into that table (tblModels) for these 2 fields (Modelcode and Modeldescription) to ignore duplicates. We do not want it to have duplicates records only unique records must be there. MS SQL Server 2000 used to insert only unique records but with MS SQL Server 2005, when ever new data contains duplicates records within the whole transaction rolls back and nothing gets inserted there which will be a problem for us.

Thanks.
alam.mushir-512938
alam.mushir-512938
SSC-Addicted
SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)

Group: General Forum Members
Points: 425 Visits: 256
One way this can be achieved is to put the rows to insert into a temporary table and then select from the temporary table only the rows that do not exist in the table where you want to insert the data finally.

Step 1: Select all rows to insert into myTblModels
Step 2: Select from myTblModels where row does not exists in final table.Insert these rows into final table:

Insert into tblModels(column1,column2,....)
Select *
From myTblModels t
Where Not Exists(Select 1 From tblModels f Where t.key1 = f.Key1 and t.Key2 = f.Key2.....)
Atang Mogwera
Atang Mogwera
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 12
Hi There,

Yes i do agree that this question has been revolving round and round but i would like to get help as well. I used to work on MS SQL Server 2000 and now using MS SQL Server 2005, I used to have 1 central table(tblModels) that has unique key on both "Manuf" and "ModelDes" that allow only unique records in and we're quering from that table on a daily basis. Now i have created the same table with same records and index key on both fields using MS SQL Server 2005.But when ever i try to append records that we receive daily into tblModels, if there are duplicates contained in that records, the whole transaction is rolled back and nothing get appended. How can i get rid of these?

Example of Syntax:

Insert into Server.DBName.dbo.tblModels (InvalidModelDescription,ManufacturerID)
Select distinct RawModelDes,Manuf
From tblPrepTemplate_SMS
WHERE RawModelDes NOT IN
(SELECT InvalidModelDescription
FROM "Server".DBName.dbo.tblModels where ManufacturerID IS NULL)


Select Count(*) as Count,InvalidModelDescription,ManufacturerID
From dbo.lutModels
Group by InvalidModelDescription,ManufacturerID
Having Count(*)>1
Order By InvalidModelDescription

Error Msg " it complains about duplicate records on index key fields"
Steve-3_5_7_9
Steve-3_5_7_9
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2922 Visits: 1609
First, there isn't a difference between the versions of SQL2000 or SQL2005 when it comes to PK's, or Unique index. The difference is how the objects were created in SQL2005.

From what I understand, now when you insert a record you are getting a "cannot insert duplicate record in object xxx" error and a rollback occurs, which is the correct behavior when you have a unique index, or PK.
The question is why are you inserting a duplicate record?
Are you trying to "cleanup" the duplicate records? If so, the above links will help with that.
Would you want to update the record instead, or are you always inserting?

What about logic such as:
If exists (select 1 from tablex where uniquekey = @uniquekey)
begin
update tablex set ....
end
else
begin
INSERT into tablex ...
end



Atang Mogwera
Atang Mogwera
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 12
Hey, thank u for the respond.

Yes we usually append records into that table(tblModels) on a daily basis as we do receive more than 50 000 records and duplicates records are included there,we want that to allow only unique records.

That table is actually using by different teams including our local call centers.What i need is the solution to maintain this table by not including duplicate records in order for other team member who is responsible for correcting new rocords according to our business rules. Because if we have duplicates within that table,size will grow dramatically, more work will be assigned to him to correct even the same records we've received before and which will delay our process and timing.

Please assist.
Steve-3_5_7_9
Steve-3_5_7_9
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2922 Visits: 1609
Is this a bulk insert operation, or just inserts as transactions occur?

If it's just "inserts" as a transaction occurs in the application you may want to follow logic such as.

1. I'm assuming that duplicate records currently don't exist on the table because of the key/index. If they do then you'll want to remove the duplicates by placing them in a table (DuplicateModel) using the logic that was outlined above "having count(*) > 1" This table has the same columns as the Model table but with a few additional columns (AuditDate, AuditUser, ApplicationName) or whatever will be useful in troubleshooting.
2. Place the code in a TRY CATCH block, or use the IF EXISTS (SELECT 1 from...) logic.
If record exists then you can return a message to the application saying "Record already exists for this customer", instead of performing the UPDATE that I mentioned above.
OR
If record exits you could always INSERT it into the table (DuplicateModel) mentioned in step 1. This way you'll know of the duplicate record and can record the datetime of the transaction as well as the user who inserted the record.


I think this should get you going in the correct direction.



Atang Mogwera
Atang Mogwera
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 12
Hi there,

Thanks again for the respond. Herewith is my example of a simple syntax below which i tried to use but nothing gets appended.

Insert into tblModels (Column1,Column2,Column3,Column4,...)
Select Distinct Column1,Column2,Column3,Column4,....
From tblReceivedData
Where Column1 not in( Select Column1 From tblModels where tblModels .Column2= tblReceivedData.Column2)


Remember clustered index has been set onto both column1 & Column2. I just need to get this work but transaction get rolled back when it reached duplicate value.

Regards,
Atang
Steve-3_5_7_9
Steve-3_5_7_9
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2922 Visits: 1609
How about:

Insert into tblModels (Column1,Column2,Column3,Column4,...)
SELECT
n.COL1, n.COL2, n.COL3, n.COL4, ...
FROM tblReceivedData n
where
NOT EXISTS
(SELECT o.col1, o.col2 from tblModels o
where n.col1 = o.col1
and n.col2 = o.col2)



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search