You cannot add identity to existing table. Identity has to be specified as part of table creation. You have two options.
- Recreate new table with identity properly set up with reseed value & Set up IDENTITY_INSERT ON & Insert existing rows to new table & IDENTITY_INSERT OFF.
CREATE TABLE [dbo].[NewTable](
[ID] [int] IDENTITY(1000,1) NOT NULL PRIMARY KEY, -- 1000 is Reseed value
Col1
Col2
);
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT dbo.NewTable ON;
GO
INSERT INTO NewTable(Id,col1,col2...)
SELECT Id, col1, col2... FROM oldtable;
GO
SET IDENTITY_INSERT dbo.NewTable OFF;
GO
- Create a sequence with starting value as the reseed value and use the sequence for your future insertions.
CREATE SEQUENCE dbo.TestSeq
START WITH 1000 -- Reseed value
INCREMENT BY 1 ;
GO
INSERT Test.TestTable (ID, Col1, Col2,...)
VALUES (NEXT VALUE FOR dbo.TestSeq, 1, 2,...) ;
GO