Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

We have same structure master table and a staging table with 30 columns, we are trying to load the data from staging to master table.In master table have 6 primary key columns but while loading the data it show primary key violation error.How to identify the duplicate records.

Thanks,
Ramesh
Posted
Comments
Maciej Los 19-Apr-13 14:54pm    
Please, be more specific and provide more details (example data).
gvprabu 20-Apr-13 4:35am    
Give the table Structure and data... then only we will try to solve

SQL
SELECT * INTO temp_duplicates FROM tbl_master

INSERT INTO temp_duplicates
SELECT * FROM tbl_staging

--to identify duplicates
SELECT id,name,COUNT(*) 
FROM temp_duplicates
GROUP BY id,name --pk columns in master
HAVING COUNT(*)>1
GO

--insert non duplicate records
INSERT INTO tbl_master
SELECT * FROM temp_duplciates 
WHERE NOT EXISTS(SELECT 1 FROM tbl_master WHERE tbl_master.id = temp_duplciates.id AND tbl_master.name = temp_duplciates.name)

DROP TABLE duplicates

[Edit]Code block added[/Edit]
 
Share this answer
 
v3
Try:
SQL
SELECT m.* FROM Master m JOIN Staging s ON m.Id = s.Id
 
Share this answer
 
Comments
ramesh4128 19-Apr-13 14:08pm    
while loading only it get error and i have almost 6 key column in master table and Staging table doesn't have any key column
OriginalGriff 19-Apr-13 14:25pm    
So list your primary key columns instead of the one I showed...
First of all, try to delete primary key (PK) one by one. When you remove first PK, try to use SELECT statement on your table to check for errors. If it wasn't helpful, remove next PK, until success.

Warning: Please, be careful. Removing PK's, you can lose integrity of the data!!!

When you successfully fetch data, try to restore PK's and - also one by one - again use SELECT statement. By doing in this way, you can find the column which raises error ;)

I can't give you an example query, because you did not provide enough information ;(
 
Share this answer
 
You can take distinct of primary keys & its count.
So if count is more than 1. Then its duplicate.
Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900