Post has been published on Varinder Sandhu 's Blog
Post has been published on http://www.varindersandhu.in/2011/02/01/add-the-value-into-an-identity-column-in-sql-server/
Add the value into an Identity Column in SQL Server
Identity columns are commonly used as primary keys in database tables.
These columns automatically assign a value for each new row inserted.
But sometime Identity columns missed the value, we want to insert missed value into the column. For that we need to enable IDENTITY_INSERT for the table.
CREATE TABLE myTable
(
myIdentity INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
myValue NVARCHAR(30) NOT NULL
)
GO
<!-- google_ad_client = "pub-2404605494811633"; google_alternate_color = "FFFFFF"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "FFFFFF"; google_color_link = "0000FF"; google_color_bg = "FFFFFF"; google_color_text = "000000"; google_color_url = "008000"; google_ui_features = "rc:6"; //-->
INSERT myTable(myIdentity, myValue)
VALUES (5, ‘Varinder Sandhu’)
–Result = Error because IDENTITY is OFF
–Below enable IDENTITY_INSERT
SET IDENTITY_INSERT myTable ON
INSERT myTable(myIdentity, myValue)
VALUES (5, ‘Varinder Sandhu’)
SET IDENTITY_INSERT myTable OFF
Once we enabled IDENTITY_INSERT on a table you must specify a value for the identity column.
–If IDENTITY_INSERT is OFF then use below insert statement for above example
INSERT myTable(myValue)
VALUES (‘Varinder Sandhu’)