简体   繁体   English

与视图相关的数据库对象-SQL Server 2005

[英]Database object related to the view - sql server 2005

How to know that how many database tables, views or any other object attached to a view in sql server 2005 database. 如何知道SQL Server 2005数据库中有多少数据库表,视图或任何其他对象附加到视图。 I tried with Sp_Depends, but amazingly it is not showing the SP name in which it is getting used. 我尝试使用Sp_Depends,但令人惊讶的是,它没有显示使用它的SP名称。

When I run the statement Sp_depends vw_MyViewName. 当我运行语句Sp_depends vw_MyViewName时。

I am only getting the name of the tables and columns which I have used inside the vw_MyViewName. 我只得到vw_MyViewName内部使用的表和列的名称。 I need to know other objects related to this view. 我需要了解与此视图相关的其他对象。

In SQL Server 2005 this happens if you create the objects in the wrong order. 在SQL Server 2005中,如果以错误的顺序创建对象,则会发生这种情况。

You will get a warning message that the dependency information could not be added for a missing object but the object will still be created 您将收到一条警告消息,提示无法为缺少的对象添加依赖项信息,但仍会创建该对象

You can run sp_refreshsqlmodule on all objects in your database to recreate such missing dependency information (an example script to do that is here How do I find all stored procedures that insert, update, or delete records? ) 您可以在数据库中的所有对象上运行sp_refreshsqlmodule来重新创建此类缺少的依赖关系信息(此处是一个示例脚本,我该如何找到所有插入,更新或删除记录的存储过程?

The only completely reliable way I know of to determine object dependencies in a SQL Server database is to load your schema into Visual Studio Database Edition (DBPro) and examine the dependencies there. 我知道确定SQL Server数据库中对象依赖项的唯一完全可靠的方法是将架构加载到Visual Studio Database Edition(DBPro)中并检查那里的依赖项。 I have found this to be foolproof, unlike the way SQL Server tracks dependencies. 我发现这很简单,与SQL Server跟踪依赖项的方式不同。

I wouldn't necessarily fault SQL Server for this. 我不一定会为此错SQL Server。 I don't think it ever made the claim that it was able to track dependencies with 100% accuracy, mostly because of the way it binds objects. 我认为它从未声称能够以100%的精度跟踪依赖关系,这主要是因为它绑定对象的方式。

In SQL Server Management Studio, in Object Explorer panel, right click on the object you want to inspect (stored procedure, table, view, ...) and click on "Show Dependencies". 在SQL Server Management Studio中的“对象资源管理器”面板中,右键单击要检查的对象(存储过程,表,视图等),然后单击“显示依赖项”。

The window that appear, will show you both dependant and "depended" objects, simply by switching between two radio button ;) 出现的窗口,只需在两个单选按钮之间切换即可显示依赖和“依赖”对象;

If you want to do it by hand, you need to build a query over the sys.sql_dependencies system view. 如果要手动执行此操作,则需要在sys.sql_dependencies系统视图上构建查询。 Here a link to the description with some examples 这里是一些说明的链接

Beware that stored procedures that depends on nonexistants tables, view, and other objects, will be created, but not only they obviously doesn't work, but dependency informations will not be added, until all "depended" objects are created, AND the SP is REcreated! 请注意,将创建依赖于不存在的表,视图和其他对象的存储过程,但不仅它们显然不起作用,而且依赖项信息也不会添加,直到创建了所有“依赖”对象,并且SP重新创建!

Until sys.sql_expression_dependencies was introduced (SQL Server 2008), you need query sys.sql_modules 在引入sys.sql_expression_dependencies(SQL Server 2008)之前,您需要查询sys.sql_modules

SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE definition LIKE '%MyTable%'

Personally, I'd use WITH SCHEMABINDING to ensure dependencies must exist 就个人而言,我将与SCHEMABINDING一起使用以确保必须存在依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM