If you want to delete all data in a SQL Server database and reseed the tables that contain a identity column, you can use the following code:

exec sp_msforeachtable "alter table ? nocheck constraint all";
exec sp_msforeachtable "delete from ?";
exec sp_msforeachtable "if exists(select 1 from sys.identity_columns where object_name(object_id) = '?') begin dbcc checkident ( '?', reseed, 0) end";
exec sp_msforeachtable "alter table ? with check check constraint all";

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts