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
89
votes
8 answers

How do you check if IDENTITY_INSERT is set to ON or OFF in SQL Server?

I've searched for this, but threads in which it appeared tended to have answers from people who didn't understand the question. Take the following syntax: SET IDENTITY_INSERT Table1 ON How do you do something more like this: GET IDENTITY_INSERT…
Panzercrisis
  • 4,590
  • 6
  • 46
  • 85
79
votes
9 answers

SqlBulkCopy Insert with Identity Column

I am using the SqlBulkCopy object to insert a couple million generated rows into a database. The only problem is that the table I am inserting to has an identity column. I have tried setting the SqlBulkCopyOptions to SqlBulkCopyOptions.KeepIdentity…
FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55
25
votes
2 answers

script to add and remove auto-increment property from a column

For a sql script I'm working on, I need to programmatically remove the identity, identity seed, and identity increment for a column in an existing table, then add them back to the table at the end of the script. Does anyone have a reference or an…
quillbreaker
  • 6,119
  • 3
  • 29
  • 47
21
votes
5 answers

How to automatically reseed after using identity_insert?

I recently migrated from a PostgreSQL database to a SQL Server database. To switch the data over I had to enable IDENTITY_INSERT. Well come to find out that I get all sorts of strange errors due to duplicate identity values(which are set as primary…
Earlz
  • 62,085
  • 98
  • 303
  • 499
15
votes
8 answers

Why does this EF insert with IDENTITY_INSERT not work?

This is the query: using (var db = new AppDbContext()) { var item = new IdentityItem {Id = 418, Name = "Abrahadabra" }; db.IdentityItems.Add(item); db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Test.Items ON;"); …
ProfK
  • 49,207
  • 121
  • 399
  • 775
13
votes
6 answers

IDENTITY INSERT and LINQ to SQL

I have a SQL Server database. This database has a table called Item. Item has a property called "ID". ID is the primary key on my table. This primary key is an int with an increment value of 1. When I attempt to insert the record, I receive an…
user208662
  • 10,869
  • 26
  • 73
  • 86
12
votes
1 answer

Using IDENTITY_INSERT with EF4

I am trying to create a record in the db that has a predefined primary key value. I know how to do this with sql, but I was wondering if EF can do this for me? Otherwise, I will have to create a stored proc for the inserts.
Paul Knopf
  • 9,568
  • 23
  • 77
  • 142
11
votes
2 answers

How to put SET IDENTITY_INSERT dbo.myTable ON statement

What I need to do is have a SET IDENTITY_INSERT dbo.myTable ON statement, what's the syntax of using the above statement in a c# app?
samsam114
  • 987
  • 2
  • 8
  • 20
10
votes
3 answers

Which command would replace IDENTITY INSERT ON/OFF from SQLServer in Oracle?

I have to migrate this query (simplified here) from T-SQL to ORACLE SET IDENTITY_INSERT table ON INSERT INTO table (id, value) VALUES (1, 2) SET IDENTITY_INSERT table OFF id being an Identity field in SQLServer. I have the same table with a…
rodrigoq
  • 509
  • 1
  • 6
  • 16
8
votes
1 answer

Cannot set IDENTITY_INSERT in batch

Case Currently I'm working on database seeding script, executed with sqlcmd. For example this script sample: IF (SELECT COUNT(*) FROM Genders)=0 BEGIN PRINT N'Seeding table Genders...' SET IDENTITY_INSERT Genders ON GO INSERT INTO…
Herman Cordes
  • 4,628
  • 9
  • 51
  • 87
7
votes
1 answer

SQL Server 2019 Behavior change with scope of SET IDENTITY_INSERT ON/OFF

We are migrating to SQL Server 2019 RTM version and noticed that one of our stored procedures that uses SET IDENTITY_INSERT ON/OFF statements are failing which works properly in SQL Server 2017 and earlier. Even changing the compatibility level to…
Gan
  • 111
  • 6
7
votes
1 answer

How to switch between DatabaseGeneratedOption.Identity, Computed and None at runtime without having to generate empty DbMigrations

I am migrating a legacy database to a new database which we need to access and "manage" (as oxymoronic as it might sound) primarily through Entity Framework Code-First. We are using MS SQL Server 2014. The legacy database contained some tables with…
7
votes
1 answer

Hibernate 3.5 vs 4 IDENTITY_INSERT issues

We run a Spring 3.1/Hibernate 4/Java 7/Tomcat 7/MSSQL 2008 R2 web application. We must deal with legacy data and archived data. When extracting data from an archive, we have a need to use the original unique identifier so that other (non-archived)…
OrangeWombat
  • 315
  • 4
  • 11
6
votes
7 answers

Linq to SQL: Why am I getting IDENTITY_INSERT errors?

I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing. There is an error inserting the identity field: Cannot insert explicit value for identity column in table 'Rigs' when IDENTITY_INSERT is set to OFF. The only…
tsilb
  • 7,977
  • 13
  • 71
  • 98
5
votes
2 answers

Turning IDENTITY_INSERT ON on a table to load it with DB Unit

I try to load a table, that have an identity column, with DB Unit. I want to be able to set the id value myself (I don't want the database generate it for me). Here is a minimal definition of my table create table X ( id numeric(10,0) IDENTITY…
Octave
  • 351
  • 2
  • 11
1
2 3 4 5 6 7