How to Truncate All the Table in Database

Data in Table which have foreign key relationships are not deleted so first Disable all the constraints and delete all the recode and then Re-enable constraints. 

-- 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"

If some of the tables have identity columns we may want to reseed them

EXEC sp_MSforeachtable "DBCC CHECKIDENT ( '?', RESEED, 0)"

 

Ebook Download
View all
Learn
View all