Identity column starts from zero instead of one
I was experiencing a strange problem. Identity field of a table is set to [1,1] by generating script. Every time, when I drop the whole database, and then re-create it by running script in SQL Server 2005 Management Studio, and then insert a record into that table programmatically, SQL Server set identity field value of that first record to 0 instead of 1!
I was gone mad until I have comeup with a small piece of code like this. The code will delete all the rows from all the tables in your database. So use it cautiously.
CREATE PROCEDURE [dbo].[ReSeed] AS
BEGIN
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1
BEGIN TRY
TRUNCATE TABLE ?
DBCC CHECKIDENT (''?'', RESEED, 1)
END TRY
BEGIN CATCH
DELETE FROM ?
DBCC CHECKIDENT (''?'', RESEED, 0)
END CATCH
'
END
Enjoy!










