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
2
votes
1 answer

IDENTITY_INSERT ON inside of cursor does not allow inserted id

I am trying to set some id's for a bunch of rows in a database where the id column is an identity. I've created a cursor to loop through the rows and update the ids with incrementing negative numbers (-1,-2,-3 etc). When I updated just one row…
Scott Mackay
  • 1,194
  • 10
  • 34
2
votes
1 answer

SQL Server 2008 R2 - why does my temp table think it has an identity column?

I'm trying to create a stored procedure that will be able to limit the number of records returned by using the department id. I'm trying to limit the records by joining to a temp table. When I run the code below I get the error: An explicit value…
user3033282
  • 158
  • 1
  • 12
2
votes
3 answers

I set IDENTITY_INSERT to ON but I get a SqlException saying it's OFF

I'm trying to grab thousands of invoices (and other stuff) from a text file and insert them into SQL Server 2008. I wrote a little console app to do this and it uses LINQ to SQL. After I insert all the existing invoices I want the Invoice_ID to be…
Ecyrb
  • 2,060
  • 3
  • 26
  • 44
2
votes
1 answer

Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF

I'm trying to insert records in a table that has got an identity column. For various reasons, this should not be a straight insert, but it should have the same ID as the foreign table. So I run: EXECUTE ('SET IDENTITY_INSERT…
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
2
votes
2 answers

SQL Server Generate Scripts with Identity Insert

When generating database scripts, I'm scripting data to be migrated to a different environment. Is there a setting in the script generation that I can enable to set IDENTITY_INSERT on/off automatically so I don't have to go through each table…
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
1
vote
2 answers

SQL: No Identity feature workaround using triggers

I'm a little rusty with my triggers and what not and am trying to figure out this problem for a class: In a database TEST, tables do not have the option of the IDENTITY feature. In other words, when we insert a row into the table “Users”, we would…
Mike Gallager
1
vote
1 answer

Can't save Data into Db because Identity Insert set to off

I've looked at several questions with this problem but many of them can be solved with a quick class annotation, but I'm using metadata for my class files so I have to use ModelBuilder to configure the relationships in my Database from classes. I…
KyleAT
  • 71
  • 1
  • 8
1
vote
1 answer

Cannot insert into identity column of linked server

Even when I set the IDENTITY_INSERT ON and specify the columns, I still get an error when inserting to a linked server. When on the server directly: CREATE TABLE InsertTest ( PrimaryAutoKey int NOT NULL IDENTITY(1, 1) PRIMARY KEY, ID…
1
vote
4 answers

I have a problem with my SQL statement

Threads ------- ThreadID UsersID Date ThreadTitle ThreadParagraph ThreadClosed Topics ----- TopicsID Theme Topics Date Here is my statement: StringBuilder insertCommand = new StringBuilder(); insertCommand.Append("DECLARE…
WithFlyingColors
  • 2,650
  • 4
  • 20
  • 25
1
vote
1 answer

Entity Framework update row while excluding IDENTITY column

I'm trying to update a row while ignoring the identity column that exists in the database. On update I get the following error: _context.Entry(existingEvent).Property(x => x.ReferenceNumber).IsModified =…
Rick
  • 109
  • 13
1
vote
3 answers

Capture IDENTITY column value during insert and use as value for another column in same transaction

While performing an insert to a table which has an IDENTITY column, is it possible to use the IDENTITY value as the value for another column, in the same transaction? For example: DECLARE @TestTable TABLE ( …
devklick
  • 2,000
  • 3
  • 30
  • 47
1
vote
1 answer

EF Cannot insert explicit value for identity column in table 'X' when IDENTITY_INSERT is set to OFF

I have to migrate data from one database to another. Keys should be preserved. I'm using EF with an auto-generated .dbmx file. I've read many answers to similar problem and follow those suggestions: I do turn IDENTITY_INSERT ON exactly before…
1
vote
3 answers

batch insertion returns disorder identity fields

I have (tableA) with one identity field (ID), I need to insert multiple rows at once with one insertion operation, so I use table structure as a passing parameter to my stored procedure I need the inserted id with the order of insertion so i'm…
Ehsan
  • 1,662
  • 6
  • 28
  • 49
1
vote
4 answers

How to avoid "SQL Server automatically uses the new inserted value as the current identity value."

I'm using SQL Server 2008 as per microsoft, http://msdn.microsoft.com/en-us/library/ms188059.aspx when I execute the following set identity_insert on //insert statements here set identity_insert off the identity of the column is set to the…
Harsha
  • 1,161
  • 4
  • 18
  • 38
1
vote
3 answers

Error: Cannot insert the value NULL into column 'ID'

I created a registration page and I have a table with a Primary key of ID,but I get this error: enter image description here Cannot insert the value NULL into column 'Id', table 'D:\课程学习\动态网站设计\课程设计1\APP_DATA\REGISTRATION.MDF.dbo.Table'; column …
Jing Cheng
  • 451
  • 1
  • 5
  • 5