You must use SET IDENTITY_INSERT TO ON. If you set it to ON, you must explicitly pass the values ββto the ID column.
Why should you disable Identity? Maybe you are trying to pass some explicit values.
Please refer to the demo here.
-- Create tool table. CREATE TABLE dbo.Tool ( ID INT IDENTITY NOT NULL PRIMARY KEY, NAME VARCHAR(40) NOT NULL ); GO -- Inserting values into products table. INSERT INTO dbo.Tool (NAME) VALUES ('Screwdriver'), ('Hammer'), ('Saw'), ('Shovel'); GO -- Create a gap in the identity values. DELETE dbo.Tool WHERE NAME = 'Saw'; GO SELECT * FROM dbo.Tool; GO -- Try to insert an explicit ID value of 3; -- should return a warning. INSERT INTO dbo.Tool (ID, NAME) VALUES (3, 'Garden shovel'); GO -- SET IDENTITY_INSERT to ON. SET IDENTITY_INSERT dbo.Tool ON; GO -- Try to insert an explicit ID value of 3. INSERT INTO dbo.Tool (ID, NAME) VALUES (3, 'Garden shovel'); GO SELECT * FROM dbo.Tool; GO -- Drop products table. DROP TABLE dbo.Tool; GO
source share