Microsoft SQL Server 2005 Administration
Best Practices

How to update statistics on all tables?

 

Execute the following script in Query Editor to update statistics with full scan on all tables:

USE AdventureWorks
GO

SET NOCOUNT ON
DECLARE @SQLcommand NVARCHAR(512), @Table sysname

DECLARE curAllTables CURSOR FOR
SELECT table_schema+'.'+table_name
FROM information_schema.tables
WHERE TABLE_TYPE = 'BASE TABLE'

OPEN curAllTables

FETCH NEXT FROM curAllTables INTO @Table

WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'UPDATING STATISTICS FOR TABLE: ' + @Table
SET @SQLcommand = 'UPDATE STATISTICS ' + @Table + ' WITH FULLSCAN'
EXEC sp_executesql @SQLcommand
FETCH NEXT FROM curAllTables
INTO @Table
END

CLOSE curAllTables
DEALLOCATE curAllTables

SET NOCOUNT OFF

GO

 

 

The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page