views:

445

answers:

5

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.

+2  A: 

I normally generate the DDL statements from the dev database and then create the database from scratch on the production server.

gkrogers
+1  A: 

We keep the scripts to build the database in version control. When we need to purge test data we drop the database and recreate it. Another option is to recover a backup that is in the state you want.

Jim Blizard
+1  A: 

I would create create/drop scripts for tables you need scrubbed and just wipe them out and recreate them.

Kon
+3  A: 

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.

Oli
+1  A: 

To reseed your identity columns, you can add the following line to your reset script.

EXEC sp_MSForEachTable 'DBCC CHECKIDENT('?', RESEED, 0)'

You can change the 0 in the command to be whatever your default value should be.

Brandon