473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

INSERT INTO... SELECT... Cannot insert duplicate key...

I want to add the content of a table into another
I tried to copy all fields, except the primary key:

INSERT INTO table2
(field2, field3, field4, ...)
SELECT field2, field3, field4, ...
FROM anotherDB.dbo.table1

gives the following error:
Violation of UNIQUE KEY constraint...
Cannot insert duplicate key...

Why?
I didn't ask him to copy the key column; Isn't the SQL Server supposed
to know how to increment the key ?
Jul 20 '05 #1
2 17847
"Caroline" <pl***@letsdothatagain.com> wrote in message
news:da**************************@posting.google.c om...
I want to add the content of a table into another
I tried to copy all fields, except the primary key:

INSERT INTO table2
(field2, field3, field4, ...)
SELECT field2, field3, field4, ...
FROM anotherDB.dbo.table1
gives the following error:
Violation of UNIQUE KEY constraint...
Cannot insert duplicate key...
The SQL Server will know how to increment the key if the field is an
IDENTITY field. Perhaps the field requires you to set your own unique key?

Tobes
Why?
I didn't ask him to copy the key column; Isn't the SQL Server supposed
to know how to increment the key ?

Jul 20 '05 #2

"Caroline" <pl***@letsdothatagain.com> wrote in message
news:da**************************@posting.google.c om...
I want to add the content of a table into another
I tried to copy all fields, except the primary key:

INSERT INTO table2
(field2, field3, field4, ...)
SELECT field2, field3, field4, ...
FROM anotherDB.dbo.table1

gives the following error:
Violation of UNIQUE KEY constraint...
Cannot insert duplicate key...

Why?
I didn't ask him to copy the key column; Isn't the SQL Server supposed
to know how to increment the key ?


There could be several reasons - a UNIQUE constraint on the target table as
well as the primary key; a trigger on the target table; a cascading foreign
key violating a constraint on another table etc.

To get a good answer, you will need to post the full DDL (the CREATE TABLE
statements) for both tables, including all keys and constraints, as well as
your exact INSERT statement.

Simon
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick | last post by:
Im trying to insert a bunch of rows into a table. If the row already exists id like to update the row 'counter'. For example... INSERT INTO table1 SELECT field1, field2 FROM table2 ON...
18
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10)...
11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
8
by: Kragen Sitaker | last post by:
ERROR: Cannot insert a duplicate key into unique index pg_class_relname_nsp_index We've been getting this error in our application every once in a while --- typically once an hour to once a day,...
8
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
2
by: mivey4 | last post by:
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...
3
by: Ashutosh Jha | last post by:
I have to Insert records through select record from a table with group by clause within a store procedure. but when i am executing the procedure, It is showing error" Violation of PRIMARY KEY...
6
by: Dave | last post by:
I really don't like the users getting an unhandled expception page, and I'm still to new with ASP.Net and C#. So please accept my appology for the 2 part question. SqlException (0x80131904) ...
1
by: rdsandy | last post by:
Hi, I have a table Risk, a table Mitigation and a table RiskArchive. I am taking the RiskID, Criticality and MitigationPlan fields from Risk, and MitigationActionID from Mitigation and inserting...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.