Pages

Sunday, May 4, 2014

How to insert values to identity column in SQL Server

Identity field is usually used as a primary key. When you insert a new record into your table, this field automatically assign an incremented value from the previous entry. Usually, you can't insert your own value to this field.
In this article, I am going to expose the tips for inserting your own value to this field. It is simple and easy. Consider you have the following Customer table.
  1. CREATE TABLE Customer
  2. (
  3. ID int IDENTITY,
  4. Name varchar(100),
  5. Address varchar(200)
  6. )
Now, I am trying to insert a record into Customer table with identity field like as then I will get the error message as shown below.
  1. INSERT INTO Customer(ID,Name,Address) VALUES(1,'Shakham','Delhi')

Allow insert into identity field

You can alllow insert to the identity field by setting IDENTITY_INSERT ON for a particular table as shown:
  1. SET IDENTITY_INSERT Customer ON

Disallow insert into identity field

You can also disalllow insert to the identity field by setting IDENTITY_INSERT OFF for a particular table as shown:
  1. SET IDENTITY_INSERT Customer OFF

Insert Value to Identity field

Now, lets see how to insert our own values to identity field ID with in the Customer table.
  1. SET IDENTITY_INSERT Customer ON
  2.  
  3. INSERT INTO Customer(ID,Name,Address) VALUES(3,'Rahul','Noida')
  4. INSERT INTO Customer(ID,Name,Address) VALUES(4,'Rahul','Noida')
  5.  
  6. SET IDENTITY_INSERT Customer OFF
  7.  
  8. INSERT INTO Customer(Name,Address) VALUES('Rita','Noida')
After Inserting your own value to identity field don't forget to set IDENTITY_INSERT OFF.

Note

  1. Usually, we use this trick when we have deleted some rows from the table and we want the data in a sequence.
  2. After Inserting your own value to identity field don't forget to set IDENTITY_INSERT OFF

Reseed the Identity field

You can also reseed the identity field value. By doing so identity field values will start with a new defined value.
Suppose you want to reseed the Customer table ID field from 3 then the new record s will be inserted with ID 4,5,6..and so on.
  1. --Reseeding the identity
  2. DBCC checkident (Customer, RESEED, 3)
  3.  
  4. INSERT INTO Customer(Name,Address) VALUES('Geeta','Noida')

What do you think?
I hope you will enjoy the tips while playing with SQL Server. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

2 comments:

  1. Try not to become a man of success, but rather try to become a man of value. See the link below for more info.


    #value
    www.ufgop.org

    ReplyDelete
  2. There are actually a variety of details like that to take into consideration. That is a nice point to bring up. I offer the thoughts above as basic inspiration but clearly there are questions like the one you carry up the place a very powerful factor will be working in sincere good faith. I don?t know if finest practices have emerged around issues like that, but I am positive that your job is clearly identified as a good game. Both girls and boys feel the influence of just a second’s pleasure, for the remainder of their lives. online casino real money

    ReplyDelete