This couple of SQL statements will delete all the rows from all tables.
- It disables referential integrity
- DELETES or TRUNCATES each table
- Enables referential integrity
- Reseeds rows with identity
— disable referential integrity
EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
GO
EXEC sp_MSForEachTable ‘
IF OBJECTPROPERTY(object_id(”?”), ”TableHasForeignRef”) = 1
DELETE FROM ?
else
TRUNCATE TABLE ?
‘
GO
— enable referential integrity again
EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’
GO
— This will reseed each table [don’t run this exec if you don’t want all your seeds to be reset]
EXEC sp_MSForEachTable ‘
IF OBJECTPROPERTY(object_id(”?”), ”TableHasIdentity”) = 1
DBCC CHECKIDENT (”?”, RESEED, 0)
‘
GO
Thanks to Mauro Cardarelli’s post
