Scenario:
We have a dbo.Customer table that has ID as identity column. We need to insert one record in this table with -1 for ID.Solution :
We will be using Identity_insert ON and Off to do this as shown below.It will disable identity property and then we will insert record and then enable it back.
Create table dbo.Customer
( ID INT Identity(1,1)
, CustomerCode VARCHAR(50)
, FirstName VARCHAR(50)
, LastName VARCHAR(50))
go
SET IDENTITY_INSERT dbo.Customer ON
GO
Insert into dbo.Customer ( ID,CustomerCode,FirstName,LastName)
Values (-1,'UNKNOWN','UNKNOWN','UNKNOWN')
SET IDENTITY_INSERT Dbo.Customer OFF
Select * From dbo.Customer