我想获取数据库中所有视图的列表及其基础表。基础表可以来自其他数据库。所以,我想知道这些表的数据库名称。那么,有人可以帮忙吗?
我尝试了下面的事情:
SELECT * 来自[INFORMATION_SCHEMA]。[VIEW_TABLE_USAGE]
但是,它没有显示表的任何数据库信息。
我还想知道数据库中存在的那些表和视图的所有者信息。那么,有人可以帮忙吗?
答案 0 :(得分:1)
有关依赖关系的信息可通过依赖关系函数和视图
获得这是使用sys.sql_expression_dependencies:
的示例SELECT DISTINCT
s.name AS view_schema_name,
v.name AS view_name,
dp.name AS [owner],
d.referenced_entity_name,
d.referenced_database_name,
d.referenced_server_name
FROM
sys.sql_expression_dependencies d
INNER JOIN
sys.views v ON d.referencing_id = v.object_id
INNER JOIN
sys.schemas s ON v.schema_id = s.schema_id
INNER JOIN
sys.database_principals dp ON s.principal_id = dp.principal_id
答案 1 :(得分:0)
此查询获取视图的表([VIEW_CATALOG]是db):
SELECT [VIEW_CATALOG] AS [database]
, *
FROM [INFORMATION_SCHEMA].[VIEW_TABLE_USAGE] AS [VIEW_TABLE_USAGE]
LEFT JOIN [sys].[tables] AS [tables]
ON [tables].[name] = [VIEW_TABLE_USAGE].[TABLE_NAME]
AND object_schema_name([tables].[object_id]) = [VIEW_TABLE_USAGE].[TABLE_SCHEMA];
本主题介绍如何获取所有者信息:List table names, owner, schema and columns in SQL server database