Home Forums SQL Server 2005 Administering Update Stats with full scan on all tables in all databases RE: Update Stats with full scan on all tables in all databases

  • You can the following query instead of the one mentioned. I see your query is consuming more cpu which is not good for prod.

    Hope you can use my query.

    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