SQL Server 2005/2008遍历所有表更新统计信息

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR 
  SELECT sst.name, 
         Schema_name(sst.schema_id) 
  FROM   sys.tables sst 
  WHERE  sst.TYPE = 'U' 
DECLARE @name   VARCHAR(80), 
        @schema VARCHAR(40) 

OPEN UpdateStatisticsTables 

FETCH NEXT FROM UpdateStatisticsTables INTO @name, @schema 

WHILE ( @@FETCH_STATUS <> -1 ) 
  BEGIN 
      IF ( @@FETCH_STATUS <> -2 ) 
        BEGIN 
                DECLARE @sql NVARCHAR(1024) 
		SET @sql='UPDATE STATISTICS ' + Quotename(@schema) 
                           + 
                           '.' + Quotename(@name)
                  EXEC Sp_executesql @sql 
        END 

      FETCH NEXT FROM UpdateStatisticsTables INTO @name, @schema 
  END 

CLOSE UpdateStatisticsTables 

DEALLOCATE UpdateStatisticsTables 

GO