Solved

How to insert row with explicit primary key

Posted on 2010-11-29
7
709 Views
Last Modified: 2012-05-10
I have a small sql ce database and I want to restore some rows from an sql script. Everything works fine, except the newly added rows are given new primary keys, even though I explicitly state them in the insert statement. I can export a row with ID = 8 but when I insert it again, I get's a new ID, how can I insert a new row and force the database to give that row a specific ID ??? Otherwise I can't restore the database and give the imported rows the correct IDs.
0
Comment
Question by:ShaunDumas
7 Comments
 
LVL 16

Accepted Solution

by:
BurnieP earned 500 total points
ID: 34234782
You need to set identity_insert on first :

SET IDENTITY_INSERT TableName ON

INSERT INTO TableName (ID) VALUES (8)
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34235008
You should be aware that you can cause some serious issues if you insert duplicate primary keys.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34236062
>>if you insert duplicate primary keys. <<
I suspect you meant duplicate identity values.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Expert Comment

by:Fahad Pathan
ID: 34236955
if u r trying to insert duplicate values in primary key column then it is not possible, but you can insert duplicate values in identity column.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34239264
@acperkins/@pathan_fahad,

reference the following quote from the author's original post:
except the newly added rows are given new primary keys, even though I explicitly state them in the insert

Even if the column is "just" an identity column and is not a PK, having a duplicate value in an "identity column" is going to mean that there will be a potential confusion with any use, whatsoever, of that identity column for purposes of identifying a given row.  (Sort of turns the concept of an "Identity column" into an oxymoron, don't you think?)

@ShaunDumas,
You can force the database to allow you to insert the value into the Identity column by the statements supplied in BurnieP's respons (#34234782); however, I would make sure that there are no rows with the same Identity value before inserting the rows.  This can be done by using something like the attached SQL.
INSERT INTO yourtable
(
 specify the columns
)
SELECT again specify the columns
FROM yourbackuptable B
LEFT OUTER JOIN yourtable Y
ON Y.identitycolumn = B.identitycolumn
WHERE Y.identitycolumn IS NULL;

Open in new window

0
 

Author Closing Comment

by:ShaunDumas
ID: 34242705
Nice!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34244740
>>Sort of turns the concept of an "Identity column" into an oxymoron, don't you think<<
No doubt.  But you have to admit that the following is a tad impossible: "insert duplicate primary keys."
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Re-appearing SQL Server Agent jobs 7 30
Nested Case statement 4 39
Database Integrity 1 51
SQL Availablity Groups Shared Path 2 16
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question