Reputation: 1
How to do a truncate statement on multiple database at once using SQL Server?
Upvotes: 0
Views: 466
Reputation: 31785
"a truncate statement"... "at once"
It's not possible to run a single statement that will truncate multiple tables on multiple databases at once.
Closest thing to it is restoring a backup of a clean (all tables empty) copy of the database. And even that will have to be done once per database.
Upvotes: 2
Reputation: 31
Below statement will take care of loop through all db's and look for one particular table and truncate the data.
exec sp_MSforeachdb 'use ?; if exists (select ''x'' from sysobjects where name =''test1'' ) TRUNCATE TABLE TEST1'
Upvotes: 0