Truncate all tables within a DB

100 Views Asked by At

I've executed the following script to delete/truncate all tables within a specific DB :

EXEC sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'DELETE FROM ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'ENABLE TRIGGER ALL ON ?'
GO

Would the script above truncate all tables in all DBs or just the DB I'm running it in?

1

There are 1 best solutions below

0
On

it will execute only in DB you are running it in. However for safety use USE yourDBName at the top of the query window

Try like this ,

USE yourDBName EXEC sp_MSforeachtable 'TRUNCATE TABLE ?'