26

如何获得 SQL Server 数据库中具有索引的视图列表(即索引视图)?

我发现在开发过程中运行“ALTER VIEW”非常容易,但忽略了我不仅在编辑视图,还删除了现有索引。所以我认为有一个小的实用查询会很好地列出我所有的带有索引的视图。

4

2 回答 2

25
SELECT o.name as view_name, i.name as index_name
    FROM sysobjects o 
        INNER JOIN sysindexes i 
            ON o.id = i.id 
    WHERE o.xtype = 'V' -- View
于 2011-02-10T21:45:47.553 回答
17

我喜欢使用较新的系统表:

select 
    OBJECT_SCHEMA_NAME(object_id) as [SchemaName],
    OBJECT_NAME(object_id) as [ViewName],
    Name as IndexName
from sys.indexes
where object_id in 
  (
    select object_id
    from sys.views
  )

内连接版本

select 
    OBJECT_SCHEMA_NAME(si.object_id) as [SchemaName],
    OBJECT_NAME(si.object_id) as [ViewName],
    si.Name as IndexName
from sys.indexes AS si
inner join sys.views AS sv
    ON si.object_id = sv.object_id
于 2015-01-25T19:22:49.957 回答