Monday, March 19, 2012

Identity seed reset in SQL table

I have a test database that is being moved to the production server. Currently in one of the tables I have an identity seed for each record. Is there a way to reset it back to zero. I have deleted all my records but it still doesnt work, and I dont want to create a new table.

ThanksCheck out DBCC CHECKIDENT. This will do what you need.

FromSQL Server 2000 Books Online:

C. Force the current identity value to 30
This example forces the current identity value in the jobs table to a value of 30.

USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED, 30)
GO

Terri|||I believe that if you issue a TRUNCATE TABLE command on the table, the identity seed will be reset to its original value.

Overall, a better strategy, since TRUNCATE TABLE is much faster than the corresponding delete statement...|||how do I do this?|||From within SQL Query Analyzer:

TRUNCATE TABLE tablename
GO

Be careful. This will delete all of the records in your table, swiftly and immediately, upon hitting that magic F5 key (or upon hitting the run arrow).|||mmmm truncate table... :) I've scared many of people with that.

Best thing to do is put that thing in a transaction just in case.

No comments:

Post a Comment