Skip to content

Instantly share code, notes, and snippets.

@antontjea
Created October 3, 2012 13:15
Show Gist options
  • Save antontjea/3826838 to your computer and use it in GitHub Desktop.
Save antontjea/3826838 to your computer and use it in GitHub Desktop.
The big clean up - Delete all data and reset all identity fields in MS Sql Server
-- disable all constraints
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
-- delete data in all tables
EXEC sp_MSForEachTable 'DELETE FROM ?'
-- enable all constraints
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'
-- reseed identity columns
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1
DBCC CHECKIDENT (''?'', RESEED, 0)
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment