This is a very handy little script to generate a script to reset the ident column of all your database tables. This can be handy after filling and truncating tables. This script uses a schema “ps”. Replace it with .dbo if needed.

SQL
SELECT '
GO
DBCC CHECKIDENT(''ps.' + TabelNaam + ''',RESEED,0)'
FROM
-- LT: Linked Tables
(SELECT O.name AS TabelNaam
, S.name AS SchemaName
FROM sysobjects AS O
LEFT OUTER JOIN sys.schemas AS S ON O.uid = S.schema_id
WHERE O.type = 'u' AND O.uid=5
AND CHARINDEX('_',O.name)=0
--ORDER BY O.[name]
) AS LT
WHERE SchemaName = 'ps'
ORDER BY TabelNaam

Usefull to export the output to text in stead of grid.

result_to_text

After running the script, copy the result to a new window and run it!

reset_ident

Frequently asked questions

How do I reset the identity column of all tables in a database?

Run the script that generates a DBCC CHECKIDENT command with RESEED,0 for every table. Copy the generated output to a new window and run it.

When do I need to reset an identity column?

The script is handy after you fill and truncate tables. It sets the identity seed of each table back to 0.

Which DBCC command resets an identity seed in SQL Server?

DBCC CHECKIDENT with the RESEED option. The script generates the line DBCC CHECKIDENT('ps.TableName',RESEED,0) for each table.

Does the reset ident script work for the dbo schema?

The script uses a schema called "ps". Replace it with dbo if you need that schema.

How do I make the generated reset script easy to copy in SSMS?

Export the output to text instead of grid. Then copy the result to a new query window and run it.

Comments

Your email address is not shown. A photo comes from Gravatar, if you have one.