首页  >  文章  >  数据库  >  SQL Server 2005/2008遍历所有表更新统计信息

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

WBOY
WBOY原创
2016-06-07 14:56:341178浏览

SQL Server 2005/2008遍历所有表更新统计信息 无 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 Updat

SQL Server 2005/2008遍历所有表更新统计信息
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
UPDATE STATISTICS tblCompany
USE tblCompany;EXEC sp_updatestats
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn