0

Possible Duplicate:
How to delete all rows from all tables in a SQL Server database?

I want to delete all rows from all tables for a specific database in a sql server which has 10's of other databases as well.

I find this post with this query but not sure how it will work

EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'DELETE FROM ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO

My SQL Server name ServerBanta and Database name is DingDong2010.

Taken from How to delete all rows from all tables in a SQL Server database?

Community
  • 1
  • 1
Developer
  • 477
  • 1
  • 4
  • 15
  • 1
    (1) **create a backup of your database!!!** and then (2) try it :-) The script disables all check constraints for all tables, and then deletes all rows from all tables. I have a hunch it won't work, though - the author of this script forgot about foreign key constraints that might still be in place ..... – marc_s Dec 13 '12 at 13:44
  • how would it know which database I want to use to delete rows from all tables :S – Developer Dec 13 '12 at 13:45
  • Well, you have to run this script **on/in a given database** (e.g. by switching into that database in SQL Server Management Studio) - so yes - you'll need which database tables will get purged ... – marc_s Dec 13 '12 at 13:46
  • 1
    see http://stackoverflow.com/questions/4115876/sql-server-delete-all-the-rows-of-all-the-tables! – Ant Dec 13 '12 at 13:47

0 Answers0