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
4
votes
2 answers

Set Identity_insert ON with start value

I work on SQL Server and I want to copy some rows from a tableA to tableB with MyColumnID included. So, on the new table I want (before the copy) to Set Identity_insert tableB ON and in the end to Set Identity_insert tableB OFF with…
S3minaki
  • 297
  • 3
  • 19
4
votes
3 answers

How to Insert Identity by AddOrUpdate methode in Seed Entityframework version 6.x.x

I have an entity that has an identity column. As part of the data-seed I want to use specific identifier values for the "standard data" in my system. I dont want disable identity. only i want to set IDENTITY_INSERT ON in migration seed. My code…
Morteza
  • 2,378
  • 5
  • 26
  • 37
4
votes
1 answer

Forcing an identity insert with NHibernate / Fluent Nhibernate

We have a fairly robust system using NHibernate, we're in the middle of migrating from a single database server to having two servers, one for administration and one for our public facing web site. This is mainly so that we can do things like…
jcavaliere
  • 125
  • 1
  • 1
  • 8
3
votes
2 answers

Sql Server Ce 3.5 Identity insert

got an issue with identity columns in Sql Server CE when using Server explorer, in VS2008, executing the following script SET IDENTITY_INSERT testTable ON; Insert into testTable (id,name) values (1,'Something') SET IDENTITY_INSERT testTable…
Bee gud
  • 147
  • 2
  • 11
3
votes
1 answer

ASP.NET MVC IDENTITY_INSERT is set to OFF

I have the following code in my HomeController: public ActionResult Create() { return View(); } [ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude =…
Cameron
  • 27,963
  • 100
  • 281
  • 483
3
votes
2 answers

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

yeah another one of these question. I have looked here but to no avail.. this is my situation. i started a ASP site, and initially was using SQLce. This turned out to be bad as the hoster didn't support it. I converted the sdf file to SQL server…
Tony
  • 53
  • 2
  • 7
2
votes
3 answers

How can I set the IDENTITY_INSERT option for a JDBC PreparedStatement?

I need to copy data into an MSSQLServer 2005 database table which has an identity column. I've seen how to disable the identity column by executing SET IDENTITY_INSERT ON before the insert queries. How can I do this when I'm using…
brabster
  • 42,504
  • 27
  • 146
  • 186
2
votes
3 answers

Force SET IDENTITY_INSERT to take effect faster from MS Access

I'm working on upsizing a suite of MS Access backend databases to SQL Server. I've scripted the SQL to create the table schemas in SQL Server. Now I am trying to populate the tables. Most of the tables have autonumber primary keys. Here's my…
mwolfe02
  • 23,787
  • 9
  • 91
  • 161
2
votes
0 answers

Identity Insert in EF6 Database-First

I am attempting to convert data from an old database into a new database, where the primary key's value is important. The code I currently have is as such (actual table name replaced with "TableName"): using (var oldContext = new OldContext()) using…
2
votes
1 answer

SQL Server remove trailing comma from XML path

I have the following code that pulls what my columns are called within a given table of mine: SELECT column_name + ',' FROM information_schema.columns WHERE table_name = 'maintReq' FOR XML PATH('') And I am wanting to place that…
StealthRT
  • 10,108
  • 40
  • 183
  • 342
2
votes
1 answer

SSIS Package Creation via MVS 2015 ODBC IDENTITY_INSERT

Today I am trying to figure out how to get rows with identity columns inserted into a Microsoft SQL 2016 database via an SSIS package that I am constructing using MVS 2015 (with SSDT 14.0.61709.290). I am pulling data from another data source…
2
votes
2 answers

SQL Server IDENTITY_INSERT Cannot find the object "TableName"

When I run in java netbeans: executeUpdate("INSERT INTO TableName (id, name) VALUES (1, 'Name1')") I get the error: Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF If I…
Julio Carvalho
  • 151
  • 2
  • 4
2
votes
0 answers

How can I insert a record with a specific identity using Linq with IDENTITY_INSERT set to ON

I'm trying to synchronize a table between two databases using linq. I want the primary keys (identities) in both tables to be the same. I haven't been able to accomplish this using linq. After scouring google, the closest I can get is this: Using…
hobwell
  • 538
  • 1
  • 8
  • 26
2
votes
0 answers

Explict Value can't be inserted in Table when IDENTITY_INSERT is OFF

I get an error when I try to insert a value in my Table. _dltype is an object of type BRIDownloadType. using (var db = new BRIDatabase()) { foreach (var client in db.BRIClients) { var todo = new BRIToDo { …
Benny
  • 258
  • 1
  • 4
  • 11
2
votes
2 answers

How to choose to use IDENTITY_INSERT or provide one in a single statement

I am working on a Stored Procedure that needs to re-insert elements in a table with an identity column. Doing this, for some rows it needs to specify the identity column, for some others it would be preferable for a new identity value the be…
MaxiWheat
  • 6,133
  • 6
  • 47
  • 76