I need to write a query that resets the identity on all tables that have an identity column and are empty.
Sql-server – Reseed identity column in all empty tables
identitysql server
Related Question
- SQL Server – How to Reseed All Identity Columns
- SQL Server – How to Remove Identity Specification for Multiple Tables
- SQL Server Identity Property – Why Removing Identity Property on a Column is Not Supported
- Changing Primary Key from IDENTITY to Persisted Computed Column Using COALESCE in SQL Server
- SQL Server Identity Seeds – Restore All IDENTITY Seeds After Database Restore
- SQL Server – Reseed Identity in Stored Procedure
Best Answer
Since you can reset the IDENTITY by issuing a simple TRUNCATE:
Though this will only work for tables not referenced by a foreign key. If you have foreign keys referencing these tables (likely), then you need: