Sql – Scrub all records in a SQL database

databasetsql

I was wondering what you would use to scrub a database of all test data (leaving the structure intact) prior to going into production?

I use something like:

-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable '
 IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
  DELETE FROM ?'
DBCC CHECKIDENT (''?'', RESEED, 0)
GO

-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO

Which I think I picked up on the net somewhere, however, it doesn't always seem to reseed everything back to zero.

Best Solution

The common approach is to have a set-up script that drops all tables and then recreates them. This has the benefit over just wiping the data of persisting any changes too.