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

  • Set Nocount on

    Declare db Cursor For

    Select name from master.dbo.sysdatabases where dbid>=11 --Doesnt include system dbs

    Declare @dbname varchar(60)

    Declare @execmd nvarchar(150)

    Open db

    Fetch Next from db into @dbname

    While @@Fetch_status=0

    begin

    if @dbname is null

    Begin

    Print 'null Value'

    end

    else

    Begin

    PRINT '###########################################################################'

    PRINT 'Update Statistics in ' + @dbname

    SELECT @execmd = 'USE ' + @dbname + ' Exec sp_msforeachtable ''Update Statistics ? with FULLSCAN'''

    EXEC(@execmd)

    PRINT ''

    End

    Fetch Next from db into @dbname

    end

    Close db

    Deallocate db

    GO

    All,

    i tried the above code and made a moification where the number of database being pulled for testing purpose is 2

    When i run it, i get the following error message:

    ###########################################################################

    Update Statistics in xxx91B

    Msg 2812, Level 16, State 62, Line 1

    Could not find stored procedure 'sp_msforeachtable'.

    ###########################################################################

    Update Statistics in xxx91C

    Msg 2812, Level 16, State 62, Line 1

    Could not find stored procedure 'sp_msforeachtable'.

    When i just run the following:

    it runs just fine

    use xxx91C

    exec sp_MSforeachtable 'update statistics ? with fullscan'

    go

    Any help would be appreciated.(SQL 08R2 RTM)

    Noli Timere