3

I've had to import some data into a db using IDENTITY_INSERT ON, do I set it back to OFF after the import? what is it's default setting? Obviously I need the application to add records/rows to the tables making a new ID each time.

1
  • This is a property of your session. Not the table. Commented Dec 26, 2013 at 19:52

2 Answers 2

8

Default setting depends on how you defined the identity column when you created the table for example

CREATE TABLE Table_Name(ID INT IDENTITY(1,1))
GO

this ID column will have a Seed value of 1 and increment by 1.

By default you cannot add values to Identity column but you can change this default behaviour by executing the following statement.

SET IDENTITY_INSERT Table_Name ON;

Once you have inserted the values then you can set it back to its default behaviour by executing the following statement.

SET IDENTITY_INSERT Table_Name OFF;

even though we can pass values to Identity column but its not a good practice, since it is an auto generated number, if you add the values into identity column yourself and then Identity column generates the same number later on you can have duplicates in your identity column.

so its best to leave identity column alone, and let it generate the values for you.

if it is necessary to add values to Identity column then I would recommend executing the following statement after every you have Indet_Insert ON and add some values to Identity column,

DBCC CHECKIDENT ( table_name, RESEED,  0) 
DBCC CHECKIDENT ( table_name, RESEED) 

The 1st statement will RESEED the value of Identity column to the smallest values in your identity column,

2nd RESEED statement with out any Seed value provided, will reseed the Identity value to the next available value to the highest value in your identity column.

Sign up to request clarification or add additional context in comments.

Comments

0

In case of someone searching for a way to create a key without identity column:

CREATE TABLE [dbo].[TableName](
    [Id] [int] NOT NULL PRIMARY KEY
)

instead of

CREATE TABLE [dbo].[TableName](
    [Id] [int] IDENTITY(1,1) NOT NULL
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.