Questions tagged [identity-insert]

`IDENTITY_INSERT` is a Sql Server database option which permits values for an identity column on a table to be explicitly specified during an Insert operation.

When using the Identity Property on a column in a Sql database, values for the column are then assigned by Sql Server, and the assigned value can be returned using SCOPE_IDENTITY() or similar.

However, in certain conditions, it may be a requirement to suppress the automatic behavior of the identity property on the column, and instead provide a user-specified value for the column.

The SET IDENTITY_INSERT option can be used to achieve this.

Example:

create table MyTable
(
 id int identity(1,1) NOT NULL,
 name nvarchar(50) NOT NULL
);

Under normal conditions, with IDENTITY_INSERT off, values for the identity column cannot be manually specified:

SET IDENTITY_INSERT MyTable OFF; -- Which is the Default
insert into MyTable(name) values ('Andrew');
-- 1 row(s) affected

insert into MyTable(id, name) values (5, 'Andrew');
-- Cannot insert explicit value for identity column in table 'MyTable' when IDENTITY_INSERT is set to OFF.

By enabling IDENTITY_INSERT, values for id MUST be provided

SET IDENTITY_INSERT MyTable ON;
insert into MyTable(id, name) values (6, 'Charles');
-- 1 row(s) affected

insert into MyTable(name) values ('Dan');
-- Explicit value must be specified for identity column in table 'MyTable' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.

References

  1. MSDN Identity(Property) http://msdn.microsoft.com/en-us/library/ms186775.aspx
102 questions
0
votes
1 answer

Can you make inserting by Id optional?

I want to be able to insert something into my table at a specific ID, so I turned IDENTITY_INSERT on for the table. However, if I just want the auto increment to handle the ID, this error appears: "Explicit value must be specified for identity…
Sven Engling
  • 39
  • 1
  • 7
0
votes
1 answer

Identity Insert just for initial Data with EF Code-First Migrations

I'm currenty trying to bring some local data into a database working with EF Code-First mirgations. These data already have unique id so initially the first 'set' of data should inserted as is. Now I'm thinking of setting…
0
votes
3 answers

Dependent insert statements

I have a table with data about a customer, Customer(name, address), with rows like "John Doe", "Some Street 123". For each row in the table, I want to insert one row in the Person(id, name) table and also one row in the Address(id, person_id,…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
0
votes
2 answers

Cannot insert explicit value for identity column while updating entity

I am using ASP.NET Boilerplate template. I want to update Details table, which contains more than one item. If an item exists, it must update, otherwise a new one must be added and all other entries relating to Master primary key in Details table…
0
votes
2 answers

Identity Insert error is being generated on insert (not in code)

I'm attempting to build a table with the following code - No errors when I create the table, I can see the struncture and it shows that IDENTITY_INSERT is set to ON. CREATE TABLE lt_percent_cs ( id_key INT IDENTITY PRIMARY KEY, …
Elizabeth
  • 719
  • 1
  • 14
  • 27
0
votes
2 answers

How to replace IDENTITY ID in table column SQL 2008?

I have accidentally deleted my record from the table. This record has a key column that is tied to some other tables in database. I have restored the Database and I can access record that I have deleted previously. When I tried to use INSERT SELECT…
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
0
votes
1 answer

MS SQL Server does not let me set IDENTITY_INSERT to ON

I have a table in a database (MS SQL Server 2008) that is filled with data. I want to move some of this data (among which is a column with an IDENTITY constraint) to a table with the same name in another database for back-up purposes. Since I have a…
Vraagteken
  • 43
  • 3
0
votes
1 answer

fatfreeframework with SQL Server databse using mapper copyfrom method with partial insert

I am attempting to insert a record using the copyFrom('POST') and save() methods of fatfreeframework v3.5. The data from POST does not contain an id field which for this table is set as an autoincrement. The SQL from the logs is SET IDENTITY_INSERT…
0
votes
1 answer

Supporting RESTful UPSERTS in Microsoft SQL

I am working on a small web application and using Microsoft SQL for the data storage. I am interfacing with the database using a service layer that leverages JDBC. Currently when I try to do a PUT for a "Customer" that does not exist by ID into the…
lmcphers
  • 468
  • 3
  • 18
0
votes
1 answer

Using LINQ in VB form, getting error: Cannot insert explicit value for identity column in table x when IDENTITY_INSERT is set to OFF

I'm just feeling my way in the world of LINQ, and I've managed to get my VB app to SELECT from my Azure DB, now I'm trying to insert a new user. I'm getting the error description 'Cannot insert explicit value for identity column in table x when…
Terry Field
  • 105
  • 9
0
votes
1 answer

EF Insert Child to specific Parent

I have been searching all over the place, found many questions but neither of them answer my. Thing I am trying to do is simple, let's say we have something like that: public class Parent { public int ID { get; set; } public List
0
votes
2 answers

proper way of updating sql server table using access front end

i have a front end in access and back end is sql server 2008 one of the fields is the account number and here are the rules it is a zipcode like 92111 plus a dash plus a number. so the first one would be 92111-1, the second 92111-2 this has to do…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
0
votes
0 answers

H2 equivalent to SET IDENTITY_INSERT

I'm using H2 to JUnit test a system that will run using MS SQL Server in production. As part of the latest version I need to make some DDL changes and migrate data from the old structure to the new. And that will involve copying data into a new…
Yaytay
  • 493
  • 4
  • 13
0
votes
1 answer

identity_insert is turned on but there is an error,yet

With following code,I have identity_insert turned on to be able to insert some value explicitly: SET IDENTITY_INSERT dbo.myTable ON Go the identity insert is set to be primary key and not null in my c# program,when i want to insert a value,this…
samsam114
  • 987
  • 2
  • 8
  • 20
0
votes
0 answers

SET IDENTITY_INSERT not working even when column names specified (coldfusion)

There are many posts about getting the error: "Explicit value must be specified for identity column in table" "either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column" The…
abalter
  • 9,663
  • 17
  • 90
  • 145