在SQLServer中是否有DMV来获取对象的依赖关系?
我正在寻找一些可以让我获得与选择“查看依赖项”+“[some_table] 依赖的对象”时显示的信息相同的信息
I'm looking for something that would get me the same information that is displayed when I select "View Dependencies" + "Objects on which [some_table] depends"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 SQL Server 2008,则以下 SQL 片段将向您显示依赖于 DBO 架构中的 FUND 表的所有对象。
SELECT QUOTENAME(S2.name) + N'.' + QUOTENAME(O2.name) AS ReferencingObject,
FROM sys.objects AS O
INNER JOIN sys.schemas AS S ON S.schema_id=O.schema_id
INNER JOIN sys.sql_expression_dependencies SED ON SED.referenced_id=O.object_id
INNER JOIN sys.objects O2 ON O2.object_id=SED.referencing_id
INNER JOIN sys.schemas S2 ON S2.schema_id=O2.schema_id
WHERE O.name='FUND' AND S.name='DBO'
请注意,从此 DMV 返回的信息不应不过,可以认为 100% 准确 - 如果您确实需要获得准确的信息,那么 Red-Gate 的 SQL Dependency Tracker 或 ApexSQL 的 Doc 产品都相当不错。
If you are using SQL Server 2008 then the following piece of SQL will show you all of the objects that depend upon the FUND table in the DBO schema.
SELECT QUOTENAME(S2.name) + N'.' + QUOTENAME(O2.name) AS ReferencingObject,
FROM sys.objects AS O
INNER JOIN sys.schemas AS S ON S.schema_id=O.schema_id
INNER JOIN sys.sql_expression_dependencies SED ON SED.referenced_id=O.object_id
INNER JOIN sys.objects O2 ON O2.object_id=SED.referencing_id
INNER JOIN sys.schemas S2 ON S2.schema_id=O2.schema_id
WHERE O.name='FUND' AND S.name='DBO'
Note that the information coming back from this DMV should not be taken as being 100% accurate though - if you really need to get accurate information then either Red-Gate's SQL Dependency Tracker or ApexSQL's Doc product are fairly good.
不不准确,看一下 你依赖 sp_depends (没有双关语) 这是我不久前写的
No not accurately, take a look at Do you depend on sp_depends (no pun intended) which I wrote a while back