Register   |  Login
You are hereArticles
 Article Details
Insert Values into an Identity Column

Is it possible to insert values into identity column of a table in SQL Server?

Yes it is possible and very simple.

Lets say there is a table Country and which has got ID, Name and Description. ID column is an  Identity column with auto increment set.

We want to import data from other SQL Server database or MS access or MS Excel or any other source. This source database has got already IDs generated. We also want to use the same IDs for some reason. Since the Country table has got ID column as Identity column, it is not possible to import, since import operation will try to insert records.

So we need to run the following query to enable the identity insert before we start import operation.

SET IDENTITY_INSERT Country ON  

You can disable the identity insert after the import operation by using the following query

SET IDENTITY_INSERT Country OFF

I hope this will help the developers who want import the data along with the identity columns.

 

 


Written By: Kumaravel
Date Posted: 1/1/2009
Number of Views: 324


Comments
You must be logged in to submit a comment.

Return