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
1
vote
1 answer

SQL Server gives "duplicate key" error for a non-key property?

I have a DB table products with 5 columns. The Id is the primary key. In SQL Server Management Studio, you can clearly see this here: I have a product with Id = 69 and Name = "Lawn Darts" . I am trying to insert a new product with Id = 420 and Name…
AlbatrossCafe
  • 1,710
  • 6
  • 26
  • 49
1
vote
1 answer

How to switch turn DatabaseGeneratedOption.Identity on/off at runtime

I have a DB that created by EF6 Code First. Some tables have an identity column. I need to insert some records to these tables with OLD Id values. I tried the solution that appeared in this link How to switch between…
1
vote
1 answer

Append impossible using RODBC::sqlSave on Azure SQL db

So there's many problems on StackOverflow with the RODBC problem, but I haven't seen this specific variant of attempting to append to an AZURE SQL db. I really think there should be a parameter that allows you to identify the KEY and NOT try to load…
Amit Kohli
  • 2,860
  • 2
  • 24
  • 44
1
vote
1 answer

SQL auto increment range or exclude numbers

I am trying to add a range or exclude numbers from an auto increment column in sql. We have an auto increment column regarding Job Numbers since each Job is unique. The problem is we treat ourselves as a client as well, and the job number as 99999…
1
vote
2 answers

Entitiy insert data."The property 'ID' is part of the object's key information and cannot be modified" error

current is new instance of myclass and context.classRepo.bringcontentwithid(userid) return an instance of it. I assign this value to my new object current then try to insert is as new row in my datatable but it gives me error like "The property 'ID'…
TyForHelpDude
  • 4,828
  • 10
  • 48
  • 96
1
vote
1 answer

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

I use Visual Studio 2013 with Sql Server 2012. I use this to generate views for my project. I have two tables: UTILISATEUR (ID_UTILISATEUR,CIN,NOM,PRENOM...,ID_ROLE): ID_UTILISATEUR is primary key and ID_ROLE is the foreign key. ROLE (ID_ROLE,…
1
vote
3 answers

Database-wide equivalent of SET IDENTITY_INSERT OFF

In my eternal saga to insert 1.4 million rows of data from a SQL script, I've written a basic WinForms app that takes each line of the script and executes it individually. However, because the original script contained SET IDENTITY_INSERT [Table]…
tags2k
  • 82,117
  • 31
  • 79
  • 106
1
vote
1 answer

SQL Server query, stored procedure multiple Identity_Insert of tables creating archived record

BEGIN IF @TransType = 1 BEGIN SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryHeaderHistory] ON -- INSERT TO DELETE DELIVERY HEADER HISTORY INSERT INTO @DRHeaderHist (RowID, CompanyName, ItemClassCode, DeliveryDate, …
1
vote
0 answers

Dropped and recreated the the table in SQL - Data not being read after that

I am a beginner level programmer who was assigned to update the data content in UI. this UI references a database table so I went ahead and began updating the table as per constraints. I had a backup of the data taken and had the create construct…
1
vote
1 answer

SQL Server Trigger using inserted ID

I'd like to create a trigger against an insert of a table in my database. So for example, if I have a company_name table, I'd like to do the following: Insert new row into company_name table on server A Invoke a trigger that gets the newly…
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
1
vote
1 answer

How to off the IDENTITY column of table in TSQL

i used 'SET IDENTITY_INSERT OFF' and this is executing successfully but in the table still identity on only please help any one
VInayK
  • 1,501
  • 6
  • 34
  • 47
1
vote
5 answers

Is the usage of identity insert good with metadatatables

I have several tables within my database that contains nothing but "metadata". For example we have different grouptypes, contentItemTypes, languages, ect. the problem is, if you use automatic numbering then it is possible that you create gaps. The…
user29964
  • 15,740
  • 21
  • 56
  • 63
1
vote
5 answers

NHibernate - Insert with identity_insert ON

I have an issue where I am trying to re-insert an entity into my database. It is illustrated with the following unit test: // entity mapped to dbo.IdentityInsertTest table // dbo.IdentityInsertTest has an IDENTITY Primary Key, Id …
Shane
  • 875
  • 1
  • 6
  • 24
0
votes
1 answer

Insert data in relationship table without identity_insert ON

I am having a scenario where I have to insert some master data into the table with script, so that we can this script on production server while deployment. My tables are Category --Id (PK, Identity) --Name CategoryType --Id (PK, Identity) …
Mahesh
  • 1,754
  • 7
  • 36
  • 54
0
votes
1 answer

ETL Strategies: Identity Insert vs. Use Identity Logic

One of my ETL moves about 18 Million rows from one server to another for further processing. I am using the FAST LOAD option. For the Identity Column, I have two options: Use IDENTITY INSERT Don't set any input for the Identity Column, thereby…
Raj More
  • 47,048
  • 33
  • 131
  • 198