LOGO

Reset Identity Column in SQL Server - How To

March 7, 2007
Reset Identity Column in SQL Server - How To

Controlling Identity Column Values in SQL Server

When utilizing identity columns within your SQL Server tables, it's possible to define the subsequent insertion value to a specific number. This is useful when you need to begin numbering your ID column at a value other than the default of 1.

Before modifying the identity value, it’s recommended to determine the current value. This can be achieved using the DBCC CHECKIDENT command.

Checking the Current Identity Value

The following command allows you to view the current identity value for a specified table without altering it:

DBCC CHECKIDENT ('tablename', NORESEED)

For example, to ascertain the next ID value for a table named 'orders', the command would be:

DBCC CHECKIDENT (orders, NORESEED)

Reseeding the Identity Column

To establish a new starting point for the identity column, the RESEED option is employed. This sets the next inserted value.

To set the next identity value to 1000, the following command can be used:

DBCC CHECKIDENT (orders, RESEED, 999)

It’s crucial to understand that the RESEED value is incremented by one to determine the next actual identity value. Therefore, reseeding to 999 will result in the next inserted row receiving an ID of 1000.

Considerations for Table Names

In certain scenarios, it may be necessary to enclose the table name within single quotes or square brackets. This is particularly relevant when referencing the table using a fully qualified path or if the table name contains spaces.

However, it is generally best practice to avoid spaces within table names to prevent potential issues.

An example of using a fully qualified table name with DBCC CHECKIDENT is:

DBCC CHECKIDENT ( 'databasename.dbo.orders',RESEED, 999)

By utilizing these commands, you can effectively manage and control the values generated by identity columns in your SQL Server databases.

#SQL Server#identity column#reset identity#DBCC CHECKIDENT#seed#increment