如何获取列上存在的统计信息(如果有)?

时间:2008-10-17 05:33:27

标签: sql-server-2005 tsql statistics

如果表中的特定列具有统计信息,我想检查Transact SQL,如果是,则要检查所有内容。

1 个答案:

答案 0 :(得分:2)

此查询应该这样做 我在存储过程中使用它来浏览数据库以查找统计数据 适用于SQL Server 2005以及可能的旧版本。

SELECT S.NAME
FROM   SYS.OBJECTS AS O
       INNER JOIN SYS.STATS AS S
         ON O.OBJECT_ID = S.OBJECT_ID
       INNER JOIN SYS.STATS_COLUMNS AS SC
         ON SC.OBJECT_ID = S.OBJECT_ID
            AND S.STATS_ID = SC.STATS_ID
WHERE  (O.OBJECT_ID = OBJECT_ID('MyTable','local'))
       AND (O.TYPE IN ('U'))
       AND (INDEXPROPERTY(S.OBJECT_ID,S.NAME,'IsStatistics') = 1)  /* only stats */
       AND (COL_NAME(SC.OBJECT_ID,SC.COLUMN_ID) = 'MyColumn')