How to reseed an ID autoincrement column in Microsoft SQL Server

If a table has been copied from one database to another, or the ID autoincrement column needs to be switched off for identity row insert, you can reseed the ID column to increment from a defined value.

Using the following queries:

DECLARE @NewID BIGINT = (SELECT MAX(ID) FROM tblMyTableName);
DBCC CHECKIDENT (tblMyTableName, RESEED, @NewID);

The first query gets the MAX value from the ID column, then the second query will use that value to reset autoincrement starting from that value.

Published: Friday 1st December 2023 - 5:27pm