Update All Statistics with Fullscan
A script that forces a full-scan statistics update on every table regardless of the row modification counter.
UPDATE STATISTICS normally only fires when enough rows have changed (the rowmodctr threshold). When you need everything refreshed right now - after a big data change, before a maintenance window, or while chasing a plan problem - this script generates and runs UPDATE STATISTICS ... WITH FULLSCAN for every base table in the current database.
DECLARE @tSQL VarChar(Max)
DECLARE Scripts CURSOR FOR
SELECT
'UPDATE STATISTICS [' + T.TABLE_SCHEMA + '].[' + T.TABLE_NAME + '] WITH FULLSCAN'
FROM
INFORMATION_SCHEMA.TABLES AS T
WHERE
T.TABLE_TYPE = 'BASE TABLE'
OPEN Scripts
FETCH FROM Scripts INTO @tSQL
WHILE(@@FETCH_STATUS = 0)
BEGIN--WHILE
----------------------------------------------------------------------------------------
RAISERROR (@tSQL, 10, 1) WITH NOWAIT
EXEC(@tSQL)
----------------------------------------------------------------------------------------
FETCH FROM Scripts INTO @tSQL
END--WHILE
CLOSE Scripts DEALLOCATE Scripts