Reset SQL Server Identity Fields

A lot of times after doing extensive testing with a website and its corresponding database, I find it annoying that the identity fields (those commonly called the auto-increment or autonumber fields) have run up their increment to a pretty high number, even though I have deleted all the rows out of them. So, after some research, I stumbled upon a way to reset it back to zero without dropping the table. As a side note, this method is valid for both SQL Server 2000 and 2005.

First things first, pull up a query window inside of SQL server. Then, run the following query and modify tablename to fit the particular table that needs its identity reset:

DBCC CHECKIDENT(tablename,RESEED,0)

Go ahead and execute the query and your identity fields should be good to go!

Print | posted on Monday, December 18, 2006 7:54 AM

Comments on this post

# re: Reset SQL Server Identity Fields

Requesting Gravatar...
Mannnn,

You saved my life ^^

thank you.
Left by Maskime on Feb 03, 2009 8:07 AM

# re: Reset SQL Server Identity Fields

Requesting Gravatar...
thanks, whoever you are. its my first few months w/ sql server. =)
Left by r.a.trigid on Jul 15, 2009 1:18 AM

# re: Reset SQL Server Identity Fields

Requesting Gravatar...
This worked flawlessly on SQL Server 2005. Thanks for the tip!
Left by Nick on Feb 04, 2010 11:51 AM

# re: Reset SQL Server Identity Fields

Requesting Gravatar...
Auto Increment Field or Identity Field In MS SQL Server

Creating a Auto Increment field in SQL Server allows a unique number to be generated when a new record is inserted into a table.

Syntax for creating an Auto Increment field in SQL Server.

CREATE TABLE [dbo].[Company](
[CompanyId] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](200) NOT NULL,
[Address] [nvarchar](max) NOT NULL,
[Phone] [nvarchar](max) NULL,
[autoBackup] [bit] NOT NULL,
[applycreditlimit] [bit] NULL,
[EmailId] [nvarchar](max) NULL,
[salesTaxPercentage] [numeric](18, 2) NULL)
Here, CompanyId has been set as an Auto Increment Id.
A column which has datatype int, big int, tiny int or small int can only be set as an Auto Increment field.
Suppose, you would like to set the AutoIncrement field (CompanyId) at the remote databases to only use the next number free AFTER the 100.

Here, CompanyId has been set as an Auto Increment Id.
A column which has datatype int, big int, tiny int or small int can only be set as an Auto Increment field.
Suppose, you would like to set the AutoIncrement field (CompanyId) at the remote databases to only use the next number free AFTER the 100.

There’s an “Identity Seed” parameter that you can specify (either through SQL or through the table designer in Enterprise Manager) that will let you set the base value for an Identity/AutoNumber field to the value you want. Any new Identity values will be incremented from this base (using the “Identity Increment” value which you can specify in the same place to determine how much to grow the value by).

If you would like to see a video as to how to set the auto increment field in SQL Server, then we have a video tutorial uplaoded at:

http://www.industrialtrainingkolkata.com/?cat=1
Left by Akhil on Mar 05, 2011 2:45 AM

Your comment:

 (will show your gravatar)
 
Please add 4 and 7 and type the answer here: