Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Primary key violation during insertion

Author  Topic 

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-02-20 : 18:48:19
I am tranferring some data from an old database to a new one.
The table structure is little different. So, I can't just move the whole data to the new one.
When doing this - I found that the old one has some duplicate entries not liked by my new key. Here's the query:
INSERT INTO ABC_LOG (ABC_NUM,ABC_TYPE,ABC_STATION,ABC_PARAM,ABC_START_DATE,ABC_START_TIME,ABC_METHOD,ABC_PARAM_LEVEL,ABC_PARAM_UNIT,ABC_GUIDE_LEVEL,ABC_GUIDE_UNIT,.............)
SELECT XYZ_SAMPLE_NUM,XYZ_TYPE,XYZ_STN_ID,XYZ_PARAM_ID, XYZ_START_DATE,XYZ_START_TIME,XYZ_METHOD,XYZ_PARAM_LEVEL,
XYZ_PARAM_UNITS,............. FROM XYZ_LOG,STN_INFO
WHERE XYZ_SITE_ID = STN_PLOTSITE_ID AND XYZ_PLOT_ID = STN_PLOT_ID

Among the fields in ABC_LOG table, first 6 are primary key fields.
If I use DISTINCT instead of just select, I get less duplication - but I still get some and using DISTINCT on all these columns is not correct because all of them are not the key fields or unique constrained.

Any idea how I can do it? I was thinking like
1) if it is possible to select each row, then iterate and see if there is any duplication based on those 6 fields - if not, insert.
2) I transferred the data to a temp table. So, is it possible to just DELETE the duplicate entries from this table and then do the insert? How can I delete duplicate entries from a table?

Thanks.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-20 : 19:16:41
Are you using SQL 2005? Try with ROW_NUMBER() over(partition by pk.columns)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-02-20 : 20:37:46
post the ddl of the tables

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-21 : 02:28:29
quote:
Originally posted by sqlbug

I am tranferring some data from an old database to a new one.
The table structure is little different. So, I can't just move the whole data to the new one.
When doing this - I found that the old one has some duplicate entries not liked by my new key. Here's the query:
INSERT INTO ABC_LOG (ABC_NUM,ABC_TYPE,ABC_STATION,ABC_PARAM,ABC_START_DATE,ABC_START_TIME,ABC_METHOD,ABC_PARAM_LEVEL,ABC_PARAM_UNIT,ABC_GUIDE_LEVEL,ABC_GUIDE_UNIT,.............)
SELECT XYZ_SAMPLE_NUM,XYZ_TYPE,XYZ_STN_ID,XYZ_PARAM_ID, XYZ_START_DATE,XYZ_START_TIME,XYZ_METHOD,XYZ_PARAM_LEVEL,
XYZ_PARAM_UNITS,............. FROM XYZ_LOG,STN_INFO
WHERE XYZ_SITE_ID = STN_PLOTSITE_ID AND XYZ_PLOT_ID = STN_PLOT_ID

Among the fields in ABC_LOG table, first 6 are primary key fields.
If I use DISTINCT instead of just select, I get less duplication - but I still get some and using DISTINCT on all these columns is not correct because all of them are not the key fields or unique constrained.

Any idea how I can do it? I was thinking like
1) if it is possible to select each row, then iterate and see if there is any duplication based on those 6 fields - if not, insert.
2) I transferred the data to a temp table. So, is it possible to just DELETE the duplicate entries from this table and then do the insert? How can I delete duplicate entries from a table?

Thanks.


why post duplicate?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=120083
Go to Top of Page

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-02-21 : 12:41:23
Yes - I am using SQL 2005. But can you please explain how I do this
"ROW_NUMBER() over(partition by pk.columns)"?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-22 : 00:58:49
quote:
Originally posted by sqlbug

Yes - I am using SQL 2005. But can you please explain how I do this
"ROW_NUMBER() over(partition by pk.columns)"?


did you try solution i posted in other link?
Go to Top of Page
   

- Advertisement -