如何获得 SQL Server 数据库中具有索引的视图列表(即索引视图)?
我发现在开发过程中运行“ALTER VIEW”非常容易,但忽略了我不仅在编辑视图,还删除了现有索引。所以我认为有一个小的实用查询会很好地列出我所有的带有索引的视图。
如何获得 SQL Server 数据库中具有索引的视图列表(即索引视图)?
我发现在开发过程中运行“ALTER VIEW”非常容易,但忽略了我不仅在编辑视图,还删除了现有索引。所以我认为有一个小的实用查询会很好地列出我所有的带有索引的视图。
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
我喜欢使用较新的系统表:
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