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 identity insert error

I set the identity inset on with "identity increment =1, identity seed=1". but when I tried it, the first id is 1, the second id is 4, the third is 8, the fourth is 14. Why is it doing this?
Julie Qin
  • 13
  • 3
0
votes
2 answers

Sql Script Error in Stored Procedure for Insert Entry

I'm working on a stored procedure to bulk add entries to a table. Here's what I've got thus far: use [DebugDatabase] go set ansi_nulls on go set quoted_identifier on go create procedure [dbo].[AddMultipleErrors] @errors ErrorTableType…
0
votes
1 answer

EF4 insert data via scripts

I know that by default EF Code First sets StoreGeneratedPattern = Identit. It's OK. But I want to insert some part of data via sql script. I know that just one table in time can be SET IDENTITY_INSERT Rules ON; But when I try to run USE…
Ray
  • 1,788
  • 7
  • 55
  • 92
0
votes
1 answer

Identity insert few records without truncating to an existing table

I would like to insert data from a table which is in A server from the B server. ex: select count(*) from A.table -- 100 rows affected delete from A.table where customer_code = '100' -- 10 rows affected select count(*) from B.table -- 200 rows…
0
votes
1 answer

EF Code First and Identity Insert - One Time Identity Insert ON, rest of the time OFF

I have an application that for the most part has a simple Users table. The PK, UserID, is a Guid. I am using EF Code first, and everything is fine with the following code: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid UserID {…
Richthofen
  • 2,076
  • 19
  • 39
0
votes
1 answer

EF Code First Parent-Child insertions with identity columns (HELP)

It is very strange to me. I have two entities. SalesStatus(Parent) and Sale(Child). public class SalesStatus { [Key] public int SalesStatusID { get; set; } /* this is identity column*/ public string Status { get; set; } public…
-1
votes
2 answers

Are there any drawbacks in turning ON Identity_Insert in production sql server

I do a unit test Add/Insert a data record into a table with column name Id which is an identity column. In the unit test I manually insert the id. So I could set the Identiy_Insert to ON before the Insert statement. Are there any drawbacks keeping…
Pascal
  • 12,265
  • 25
  • 103
  • 195
-1
votes
1 answer

Adding object to another using Entity Framework, reciving an error

I'm trying to add a product to a category using this code public void AddProductToCategory() { if (categoryExist) { var product = FindProduct(productArticleNumber); var productExist…
-1
votes
1 answer

Toggle insert identity with single statement

For the really lazy on the field: is there a single statement that will toggle IDENTITY INSERT sometable from on to off and vice versa?
George Menoutis
  • 6,894
  • 3
  • 19
  • 43
-1
votes
2 answers

Is identity_insert reset to OFF at session close?

SQL server question. Although my tests say "yes", I'd rather hear it from someone with experience. When I use SET IDENTITY_INSERT some_table ON, I'd like to know that I can safely close my session without being anxious of forgetting to set it to off…
George Menoutis
  • 6,894
  • 3
  • 19
  • 43
-1
votes
3 answers

How do I insert data into a row in SQL?

I am following a tutorial and learning MVC from a book, where I was told to create a table using this script, which I did. But now I want to add an entire row to my Pet table, but I am unable to do it. Script used to create all my tables. CREATE…
John Mike
  • 147
  • 1
  • 14
-3
votes
1 answer

Why does one have to specify columns when IDENTITY_INSERT ON?

The following script failed to execute SET IDENTITY_INSERT dbo.MyTable ON -- the table only has three columns, so there's no ambiguity INSERT INTO dbo.MyTable VALUES (1, 2, 3) SET IDENTITY_INSERT OFF Which throws the following warning: An…
8protons
  • 3,591
  • 5
  • 32
  • 67
1 2 3 4 5 6
7