Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Get rid of duplicate records in a table using SQL Server 2005 Expand / Collapse
Author
Message
Posted Tuesday, June 8, 2010 2:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 18, 2010 7:43 AM
Points: 5, 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.
Post #933839
Posted Tuesday, June 8, 2010 4:55 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 7:48 PM
Points: 2,834, Visits: 8,542
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



Post #933891
Posted Wednesday, June 9, 2010 4:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 18, 2010 7:43 AM
Points: 5, 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.
Post #934523
Posted Wednesday, June 9, 2010 10:49 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 3:52 AM
Points: 413, Visits: 242
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.....)
Post #935137
Posted Thursday, June 17, 2010 8:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 18, 2010 7:43 AM
Points: 5, 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"
Post #938929
Posted Friday, June 18, 2010 6:24 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 6:50 AM
Points: 922, Visits: 1,444
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





Post #939532
Posted Friday, June 18, 2010 7:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 18, 2010 7:43 AM
Points: 5, 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.
Post #939578
Posted Friday, June 18, 2010 7:34 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 6:50 AM
Points: 922, Visits: 1,444
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.



Post #939598
Posted Friday, June 18, 2010 7:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 18, 2010 7:43 AM
Points: 5, 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
Post #939614
Posted Friday, June 18, 2010 8:27 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 6:50 AM
Points: 922, Visits: 1,444
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)



Post #939639
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse