How To Insert Value In Identity Column Manually in SQL Server Table - SQL Server / T-SQL Tutorial Part 41

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))

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


How to insert value in Identity Column manually in SQL Server Table


Video Demo :  How to manually insert the value in identity column in SQL Server Table


1 comment: