If you run the following code on a table that never had a record in it, you will the following error :
Parameter 3 is incorrect for this DBCC statement
DECLARE @max_id INT
SELECT @max_id = MAX(ID)+1 FROM testt;
DBCC CHECKIDENT('Tablename',RESEED,@max_id);
The reason is that @max_id
is NULL in this case and you can't pass it.
Here is what I would do. I would check the value returned to make sure it's not NULL. Hope this helps
DECLARE @max_id INT, @var int
SELECT @max_id = MAX(ID)+1 FROM TABLENAME;
IF @max_id IS NULL
BEGIN
SELECT@var = 1
END
ELSE
BEGIN
SELECT @var = @max_id
END
DBCC CHECKIDENT('TABLENAME,RESEED,@var);