Frank Q. Frank Q. - 4 months ago 19
SQL Question

Reseeding SQL auto increment from current value

I am trying to reseed the auto increment value from the current auto increment value of the table in SQL.
I did something like

USE [MyDatabase]
GO

SELECT IDENT_CURRENT('MyTable') AS Current_Identity
DBCC CHECKIDENT (MyTable, reseed, Current_Identity)


I get the following error:

(1 row(s) affected)
Msg 2560, Level 16, State 9, Line 3
Parameter 3 is incorrect for this DBCC statement.


Any idea whats going wrong?

Answer

In SQL Server, you could save the IDENT_CURRENT value in a variable, like this:

DECLARE @currentIdentity INT;
SELECT @currentIdentity = IDENT_CURRENT('MyTable');
DBCC CHECKIDENT (MyTable, reseed, @currentIdentity);

To answer your question, your problem is that Current_Identity is an alias to a column, but you did not store it anywhere. This means that the next call you make to DBCC CHECKIDENT cannot reference the column from the previous query.

Comments