Update Statistics
Update Statistics for All Tables and Databases in a SQL Server Instance
Last updated:
Warning: Review and test in a non-production environment before running.
DECLARE @TSQL nvarchar(2000)
-- Filtering system databases and user databases from execution
SET @TSQL = '
IF (DB_ID(''?'') > 4
AND ''?'' NOT IN(''distribution'',''SSISDB'',''ReportServer'',''ReportServertempdb'')
)
BEGIN
PRINT ''********** Rebuilding statistics on database: [?] ************''
USE [?]; exec sp_updatestats
END
'
-- Executing TSQL for each database
EXEC sp_MSforeachdb @TSQL