Search

Thursday, November 10, 2011

Delete all Data from all tables in a database


Run below query to delete all records from all tables:
EXEC sp_MSforeachtable @command1 = “DELETE FROM ?”
OR
EXEC sp_MSforeachtable @command1 = “TRUNCATE TABLE ?”
We cannot delete data from all tables if there are foreign key references. To resolve this we have to disable all constraint than run the above query. To disable all constraint run below query:

EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
Now run the query to delete data.

No comments:

Post a Comment