Update Auto Generated Primary Key Sql Server Set Identity_insert

 

Aug 15, 2019  How To Use SET IDENTITYINSERT In SQL Server. SQL Server includes IDENTITY property to generate auto-increment numbers. IDENTITY generates incrementing numbers when a record is inserted into a table. Often referred to as a surrogate key, and commonly used as a primary key to have unique incrementing values in a column. Sep 14, 2017  In Entity Framework 6 you can create an entity and set the primary key property to whatever value you want, because in case the ID is generated in database (i.e. Identity in MS SQL Server) the value will be overwritten anyway. And Entity Framework 6 will happily work with that scenario. I am designing a table and I have decided to create an auto-generated primary key value as opposed to creating my own scheme or using natural keys. I see that SQL Server offers globally unique identifiers (GUIDs) as well as identities to create these valu.

By: Greg Robidoux Updated: 2010-06-10 Comments (2) Related: More >Identities

Problem

In a previous tip, Using Identity Insert to keep SQL Server database table keys in sync, we discussed how to move data between like tables when the table has an identity column. The biggest drawback when doing this is that you need to specify each column of the table for the INSERT and also make sure the columns on the SELECT match up as well. Doing this for one or two tables is not that hard, but what if you have several tables or an entire database that you need to move and all of the tables have identity columns? In this tip we take a look at a simple way to create the INSERT command using the system meta data.

Auto
Solution

Identity columns are often used in tables as a way to create a primary or unique key on a table. When moving data between like tables that have identity columns you need to specify each column for the INSERT and the SELECT.

Let's say we have a table named Employee with the following structure and another table named Employee2 with the same structure:

You can not do a simple command such as the following to move the data:

If you run the above command and the table has an identity column you will get this error message:

The solution to get around this is that you need to specify the columns as follows:

The above example is pretty simple to put together if you only need to do one table and only have a few columns, but what if you have a bunch of tables with many columns?

The following script uses the system meta data to pull the columns and create the command for you instead of having to cut and paste or type the entire command. For this script you need to specify the schema and table names for the current and new table as shown below.

If we run the above code we get the below command that can then be run. Note in the above code that this does a PRINT of the commands instead of executing the code. If you want to execute the code just uncomment the EXEC (@sqlcmd) line of the code and this will create and execute the command.

That's all there is to it.

Next Steps
  • Take this a step further to run this for each of your tables or a batch of tables
  • Add this script to your SQL toolbox
  • Refer to the previous tip - Using Identity Insert to keep SQL Server database table keys in sync
Sql
Last Updated: 2010-06-10



About the author
Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.
View all my tips

Update Auto Generated Primary Key Sql Server Set Identity_insertt On Linked Server



By: Greg Robidoux Updated: 2006-09-26 Comments (4) Related: 1 2 3 4 More >Identities

Problem
One thing that DBAs are often faced with is moving data from one database to another database to populate lookup tables or some other key table in your database. This may be to keep a test or development environment in synch or maybe there is a need to populate like databases on other servers with the same data values.

Another thing that is often common with SQL Server is the use of identity values or auto incrementing of a key value for new records as they get inserted. Using identity values is a simple way to make sure you have a unique primary key for new records, but there is no simple way to control what identity value will be given for a certain row in a table.

When you combine these two items together, having an identity column as a primary key and having the need to push like data to other databases this is where the problems begin. If your tables on these different databases are setup exactly the same way where both tables have an identity value there is no way to control what identity value one table will get vs the other table and therefore you can have issues where the data is not in synch.

Solution
To handle this situation where you need to make sure the data on your two databases stays in synch along with having an identity value for the key, SQL Server has a command SET IDENTITY_INSERTthat allows you to turn on and off the auto generation of the identity value as needed. The command gets turned on prior to the inserting of data and gets turned off after the data has been inserted.

Here is a simple command that allows you to insert into a table that has an identity value field, but allows you to specify what the value will be instead of having SQL Server pick the next sequential value.

This first query is our regular insert into our primary database. At this point we don't need to specify the ClientID, because this value is auto generated from our identity value on the ClientID column.

INSERT INTO dbo.CLIENT (ClientName) VALUES ('Edgewood Solutions')

INSERT INTO dbo.CLIENT (ClientName) VALUES ('Microsoft')

After we insert the data we have two new records in our Client table.

ClientIDClientName
782Edgewood Solutions
783Microsoft

To move this data to our other databases that have the same table and need to retain the same identity values we issue the command below. First we turn on the identity insert, insert the two records this time including the ClientID value and then we turn off the identify insert to make sure that if any future records get added the clientID column is auto generated.

SET IDENTITY_INSERT dbo.Client ON

INSERT INTO dbo.CLIENT (ClientID, ClientName) VALUES (782, 'Edgewood Solutions')

INSERT INTO dbo.CLIENT (ClientID, ClientName) VALUES (783, 'Microsoft')

SET IDENTITY_INSERT dbo.Client OFF

By using the IDENTITY_INSERT command we are able to make sure that the records that get inserted on our other databases retain the same ClientID value, this way there are no data integrity issues from one database to another.

Next StepsDownload bad piggies activation key generator.

  • Next time you need to populate like tables on different databases that have identity values look at using this command to make the maintenance of these tables easier

Last Updated: 2006-09-26

Update Auto Generated Primary Key Sql Server Set Identity_insertt



About the author
Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.
View all my tips
Related Resources

Update Auto Generated Primary Key Sql Server Set Identity_insert Sert Off