By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

Violation of PRIMARY KEY constraint...Cannot insert duplicate key Problem

P: 37
Okay I have 2 tables:

Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes)

Table B - holds a distinct list of current serial numbers and the corresponding model numbers (primary key on serial_numbers)

Since Table A has no constraints duplicates may exist.

Additionally, table A is actually an Excel spreadsheet that is maintained by an employee that records new hardware as they come in. I am pulling the data from Table A to insert into Table B and so construct the insert query to prohibit any entries into Table B (which has a primary key set on the serial_number column) so duplicate entries aren't attempted on the insert.

insert into system_serial_numbers(serial_num,system_model)
select [sys #], [model #] from EXCEL...Master$
where [sys #] not in (select serial_num from system_serial_numbers)
and [model #] is not null

When I run this query I receive the following error and the insert fails

Violation of PRIMARY KEY constraint 'pk_serial'. Cannot insert duplicate key in object 'dbo.system_serial_numbers'.
The statement has been terminated.

If I take one of the records retrieved from the select statement and insert it into the table it succeeds which leads me to believe that maybe one of the records returned is a duplicate but theoretically this shouldn't be happening because I specified that only records not in the existing destination table be inserted.

Anyone have any ideas why this problem is happening or have ever seen this before???
Jan 28 '07 #1
Share this Question
Share on Google+
2 Replies


iburyak
Expert 100+
P: 1,017
You have duplicates in Excel that are not in your table yet. Just use distinct in select query...

insert into system_serial_numbers(serial_num,system_model)
select distinct [sys #], [model #] from EXCEL...Master$
where [sys #] not in (select serial_num from system_serial_numbers)
and [model #] is not null



Good Luck.
Jan 29 '07 #2

almaz
Expert 100+
P: 168
You have duplicates in Excel that are not in your table yet. Just use distinct in select query...
iburyak, unfortunately it may not help as unique constraint is set to serial_num field only, and you are using distinct to select unique serial_num, system_model pairs (so if there are different records with the same serial_num and different system_model values there still will be a problem).

mivey4, There is no ideal solution for your problem. If Excel file contains different system_model values for the same serial_num value, you'll have to either manually or based on some automatic logic decide what is the most appropriate value for system_model.
Following query selects these problematic records:
Expand|Select|Wrap|Line Numbers
  1. select  [sys #], [model #]
  2. from    EXCEL...Master$
  3. where   [sys #] in (select  [sys #]
  4.                     from    EXCEL...Master$
  5.                     where   [sys #] not in (select  serial_num
  6.                                             from    system_serial_numbers) and [model #] is not null
  7.                     group by [sys #]
  8.                     having  count(*) > 1) and [model #] is not null
  9.  
Following solution is one of the possible automatic solutions
Expand|Select|Wrap|Line Numbers
  1. insert into system_serial_numbers (serial_num, system_model)
  2. select [sys #], max([model #])
  3. from   EXCEL...Master$
  4. where  [sys #] not in (select  serial_num
  5.                         from    system_serial_numbers) and [model #] is not null
  6. group by [sys #]
Jan 29 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.