Tuesday, December 22, 2009

Turn off Identity Insert

To be able to insert a record into a table with a "hardcoded" ID you simply run:

SET IDENTITY_INSERT mytable ON

--run your insert code here

SET IDENTITY_INSERT mytable OFF

Tuesday, December 1, 2009

Reseed All Identity Columns in SQL Server 2005

EXEC sp_msforeachtable @command1 = 'DBCC CHECKIDENT (''?'', RESEED)'

This will get the highest record ID for each table and reset the identity seed to one number higher.

It will display any error for any table that does not have an identity column.

To do only one table you can run this:
DBCC CHECKIDENT ('tablename', RESEED)

To see what the current seed is:
DBCC CHECKIDENT ('tablename', NORESEED)