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

DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR
02   SELECT sst.name,
03          Schema_name(sst.schema_id)
04   FROM   sys.tables sst
05   WHERE  sst.TYPE = 'U'
06 DECLARE @name   VARCHAR(80),
07         @schema VARCHAR(40)
08  
09 OPEN UpdateStatisticsTables
10  
11 FETCH NEXT FROM UpdateStatisticsTables INTO @name, @schema
12  
13 WHILE ( @@FETCH_STATUS <> -1 )
14   BEGIN
15       IF ( @@FETCH_STATUS <> -2 )
16         BEGIN
17                 DECLARE @sql NVARCHAR(1024)
18         SET @sql='UPDATE STATISTICS ' + Quotename(@schema)
19                            +
20                            '.' + Quotename(@name)
21                   EXEC Sp_executesql @sql
22         END
23  
24       FETCH NEXT FROM UpdateStatisticsTables INTO @name, @schema
25   END
26  
27 CLOSE UpdateStatisticsTables
28  
29 DEALLOCATE UpdateStatisticsTables
30  
31 GO

转载于:https://www.cnblogs.com/kevinGao/p/3175981.html

猜你喜欢

转载自blog.csdn.net/weixin_34324081/article/details/93052430