DBCC CHECKINDENT RESEED can be used to reset a tables identity value on a column.
On the official Microsoft page about it, it reads
“Checks the current identity value for the specified table in SQL Server 2016 and, if it is needed, changes the identity value. You can also use DBCC CHECKIDENT to manually set a new current identity value for the identity column.”
This was an interesting find but when you might you use it?
I will create a small table to demonstrate how to use this.
T-SQL:
[sourcecode language=’sql’]
CREATE TABLE Table_1
(
ID INT IDENTITY(1,1) NOT NULL,
DateAdded DATETIME NOT NULL
)[/sourcecode]
Insert some rows….
[sourcecode language=’sql’]
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
[/sourcecode]
Check the current value which produces the below output….
[sourcecode language=’sql’]
DBCC CHECKIDENT (‘Table_1’, NORESEED);
[/sourcecode]
Checking identity information: current identity value ‘5’, current column value ‘5’.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Now we will reset the identity value so that the next time we insert data, the value will be 10….
[sourcecode language=’sql’]
DBCC CHECKIDENT(‘Table_1’, RESEED, 9);
[/sourcecode]
Checking identity information: current identity value ‘5’.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Add another row and check identity value. The row inserted will have a value of 10….
[sourcecode language=’sql’]
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
[/sourcecode]
Checking identity information: current identity value ’10’, current column value ’10’.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Scenarios when you might use DBCC CHECKIDENT
It’s already clear what the function of this is but when might you use it?
I’m nervous at the thought of this being called in a production environment but in a dev environment, sure it could come in handy if you need to reset things to a known state.
TRUNCATE TABLE is a command which will enable you to delete all rows from a table and at the same time, reset the identity value. However you need permission to be able to do this and it will only work if the table is not referenced by a foreign key constraint. This is when DBCC CHECKIDENT is useful.
Some notes about DBCC CHECKIDENT
- You need to be a db_owner (dbo) of the database. If you are not a dbo, you would need permission to execute on behalf of dbo using WITH EXECUTE AS
- Reseed as (n – 1). So if you wanted a value of 5 as your next value, reseed using 4
- Setting a value which is less than values which are already in the table will result in unique constraint violations as soon as the value inserted hits a value which already exists in the table.
- Data type limits apply, you cannot exceed the max value of a TINYINT for example when reseeding.
- Running it on an empty table produces a change in behaviour. The following demonstrates this….
[sourcecode language=’sql’]
DBCC CHECKIDENT(‘Table_1′, RESEED, 0);
[/sourcecode]
Now after running that DBCC statement and then adding a row, you would expect the next value to be a 1 but it’s not….
[sourcecode language=’sql’]
INSERT INTO Table_1(DateAdded)
VALUES(GETDATE());
DBCC CHECKIDENT (‘Table_1’, NORESEED)
[/sourcecode]
Checking identity information: current identity value ‘0’, current column value ‘0’.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
This is normal behaviour however. If the table is empty, then the next value is the reseed value as opposed to the reseed value plus the increment value.
MS says
Nice write up. Simple and helpful in explaining checkident function and quick to read. Thank you.