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

SQL Server integrity check after identity_insert on / off

I'm importing data from a legacy application (php + mysql) to a rewritten version (sql server 2008 and mvc3). I'm currently in the middle of writing a db synchronization / conversion package. The mysql data has primary keys but no foreign keys for…
Ales Potocnik Hahonina
  • 2,977
  • 2
  • 26
  • 32
0
votes
3 answers

Insert into table with Identity and foreign key columns

I was trying to insert values from one table to another from two different databases. My issue is I have two tables with a relation and the first table is having an identity column also. eg table first(id, Name) - table second(id, address) So now…
Saanch
  • 1,814
  • 1
  • 24
  • 38
0
votes
0 answers

Saving multiple row by foreach

In ASP .NET MVC 5 , I am trying to save multiple rows of permissions by the use of foreach this is my model: public class RoleClaims { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int id { get; set; } …
0
votes
1 answer

IDENTITY_INSERT Error in table I am not even trying to Insert?

I am getting this Error: SqlException: Cannot insert explicit value for identity column in table 'Unterwerke' when IDENTITY_INSERT is set to OFF. ..when I am Trying to insert something into the table "Clients". Every Client has 1 Unterwerk but i…
Herbix
  • 35
  • 6
0
votes
1 answer

Assign EF Core identity id to a property after an insert in the database has been made?

I have this method: public async Task CreateTrainingPlan(TrainingPlanDTO trainingPlan) { TrainingPlan tp = new TrainingPlan { Name = trainingPlan.Name }; // adding to context _context.TrainingPlan.Add(tp); //…
0
votes
2 answers

Setting IDENTITY_INSERT ON for SQL Server table valued type

Is there a way to set IDENTITY_INSERT ON for table valued type? The way how it is done with tables - isn't working. CREATE TYPE dbo.tvp_test AS TABLE ( id INT NOT NULL IDENTITY(1, 1), a INT NULL ); GO DECLARE @test dbo.tvp_test; SET…
Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
0
votes
4 answers

Stored Procedure IDENTITY_INSERT

I'm recently change my data table, I remove column and add a new column that define as identity = True and identity seed = 1, identity increment = 1. When i tried to insert data to this table by STORE PROCEDURE i get this exception: An explicit…
Jacob
  • 3,598
  • 4
  • 35
  • 56
0
votes
1 answer

JDBC sql server: set IDENTITY_INSERT ON: no effect

JDBC sql server: set IDENTITY_INSERT ON: no effect. PreparedStatement stmt = connection.prepareStatement("SET IDENTITY_INSERT Table_FOO ON"); stmt.execute(); stmt.close(); PreparedStatement stmt2 = connection.prepareStatement("insert into…
eastwater
  • 4,624
  • 9
  • 49
  • 118
0
votes
1 answer

Regarding: Problem setting IDENTITY_INSERT ON when trying to insert data

Good Day, I am currently having trouble with IDENTITY_INSERT. I have a linked server setup with the required permissions needed. I have tested that I can use IDENTITY_INSERT with a simple insert query, but it does not work if I use the following…
0
votes
0 answers

MVC5 problem saving data that should auto increment

I'm using a method that creates new Entries in a table. The method looks like this: public void CreateProcessingTime(Guid userId, int activityId, int min, int max, int avg) { var t = new T_USER_ACTIVITY_PROCESSING_TIME …
Peter
  • 1,844
  • 2
  • 31
  • 55
0
votes
0 answers

How do I enable identity insert of a SQL Server database in a mulesoft application?

While developing a data migration application, using Mulesoft's Anypoint Platform, I've encountered an issue with the insertion of the data of a table that has an identity attribute. To insert data on those types of tables, I usually use the command…
ooz
  • 13
  • 2
  • 9
0
votes
0 answers

Bulk insert from txt. file with primary key and Timestamp values

I have a table called Address which has an AddressID as the primary key (auto-incremented value), it also has a tmsp column which is of type TimeStamp. I created a stored procedure that deletes records from the table but before deleting, I save the…
Mindan
  • 979
  • 6
  • 17
  • 37
0
votes
0 answers

Identity_insert is set to OFF error even-though it is set to ON

I get an error Cannot insert explicit value for identity column in table 'Table1' when IDENTITY_INSERT is set to OFF I have set the IDENTITY_INSERT to ON by using the query SET IDENTITY_INSERT [Database1].[dbo].[Table1] ON, but still i get this…
user9630935
  • 349
  • 1
  • 4
  • 18
0
votes
1 answer

How to Insert into Identity column with c# code

Yes i know, there is alot of material out there on how to turn on identity insert and turn it off after, which is actually something i want to use. But there is more to my problem, thats why i started this question. The conventional way to insert…
QuestGamer7
  • 133
  • 1
  • 1
  • 8
0
votes
0 answers

How to Submit a T-SQL script

I'm new to SQL Server and was trying some SQL statements for my database. I want to enable identity insert with the following statement: SET IDENTITY_INSERT tableName ON That works fine. After that I inserted an entry but if I close the script the…