您可以使用sys.dm_sql_referenced_entities
选择对象所依赖的表和视图。请参阅联机丛书文章。
此 cte 将从出现在 sys.dm_sql_referenced_entities 结果中的视图中递归地选择表。它应该适合你。请参阅SQL 小提琴
DECLARE @viewName sysname = 'dbo.V3'
;WITH cteViews AS(
SELECT
re.referenced_id
,o.type_desc
,SCHEMA_NAME(o.schema_id) AS [schema]
,o.name
FROM sys.dm_sql_referenced_entities (@viewName, 'OBJECT') re
JOIN sys.objects o ON re.referenced_id = o.object_id
UNION ALL
SELECT
viewTables.referenced_id
,viewTables.type_desc
,viewTables.[schema]
,viewTables.name
FROM cteViews cte
OUTER APPLY(
SELECT
re.referenced_id
,o.type_desc
,SCHEMA_NAME(o.schema_id) AS [schema]
,o.name
FROM sys.dm_sql_referenced_entities (cte.[schema] + '.' + cte.name, 'OBJECT') re
JOIN sys.objects o ON re.referenced_id = o.object_id
WHERE o.type_desc = 'USER_TABLE'
) AS viewTables
WHERE cte.type_desc = 'VIEW'
)
SELECT
[schema]
,name
FROM cteViews
WHERE type_desc <> 'VIEW'
ORDER BY name
SSMS 中还有一个功能可以让您查看对象的依赖关系。右键单击对象资源管理器中的视图,然后选择“查看依赖项”。然后选择单选按钮“'视图名称'所依赖的对象”。