Reseed an Identity (Auto Increment) field within Microsoft SQL Server

© copyright 05.Jan.2009 by Paul Bradley filed under Microsoft SQL Server


[Ad] need a break from coding?

Hoseasons Villas

When I am developing a new system I like to keep my database schema in a separate SQL text file, so I can easily re-create the database while I am testing. I also keep all my test data in a SQL file, so I can easily insert all my test data after recreating a blank copy of the database.

During a demonstrate of the system to the users / clients I find it useful to re-seed the the auto increment values of certain tables before inserting my test data, so that the values used in the demonstration represent realistic values which will be used after the system has been live for several months.

To do this I use the following SQL syntax, where TABLE is replaced with the table name to change, and VALUE is replaced with the new starting value for the identity field.


DBCC CHECKIDENT (TABLE, RESEED, VALUE)
GO

So for example, if I wanted to re-seed the orders table with a starting value of 20,000 - I would use the following SQL syntax.


DBCC CHECKIDENT (ORDERS, RESEED, 20000)
GO

 


If you have found this article helpful or useful please consider linking to it, emailing it to friends, or share it with others using social sites like del.icio.us, Stumble Upon or Twitter.

Paul Bradley

About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He provides PHP & MySQL bespoke development services via his software development company, Carlisle Software Limited.
He has over 20 years programming experience.

Other Popular Articles

Categories & Topics

Home · Apache · JavaScript · Perl · PDF · PHP · MySQL · MSSQL · TAR · Ubuntu Linux · Video · Visual Basic

Browse the complete article history, and if you like what you see; consider subscribing to the rss feed.