Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Identity Seed in SQL Server (Permanently!)

Is there any way of changing the identity seed for an identity column permanently? Using DBCC CHECKIDENT just seems to set the last_value. If the table is truncated all values are reset.

dbcc checkident ('__Test_SeedIdent', reseed, 1000)

select name, seed_value, increment_value, last_value
from sys.identity_columns
where [object_id] = OBJECT_ID('__Test_SeedIdent');

returns

name      seed_value  increment_value  last_value
-------------------------------------------------
idIdent   1           1                1000

I was hoping that some syntax like

alter table dbo.__Test_SeedIdent alter column idIdent [int] identity(1000,1) NOT NULL

would exist.

Is it necessary to create a new column, move the values across, drop the original column and rename the new?

like image 742
avenmore Avatar asked Jan 26 '10 13:01

avenmore


People also ask

How do I permanently disable identity column in SQL Server?

Add a new column Execute an UPDATE statement to set the value of the new column to the value of the identity column in each row. Drop the identity column. Rename the new column to replace the original identity column.

How do I change identity properties in SQL Server?

You cannot alter a column to be an IDENTITY column. What you'll need to do is create a new column which is defined as an IDENTITY from the get-go, then drop the old column, and rename the new one to the old name.


1 Answers

From Books Online:

"To change the original seed value and reseed any existing rows, you must drop the identity column and recreate it specifying the new seed value. When the table contains data, the identity numbers are added to the existing rows with the specified seed and increment values. The order in which the rows are updated is not guaranteed."

like image 59
Tom H Avatar answered Sep 23 '22 00:09

Tom H