查找对 SQL Server 数据库中某个对象的所有引用
我正在尝试查找对 SQL Server 数据库中某个对象的所有引用。
如何快速搜索? SQL Server Management Studio 似乎没有这样做。我使用 http://www.red-gate.com/products/SQL_Search/但我想找到“官方”微软解决方案。是在其他产品中吗?
例如,当我在 Visual Studio 中进行大量搜索时,我希望能够在所有存储过程中找到某些内容。
或者也许我没有以正确的方式编码?
卡尔
I'm trying to find all references to an object in an SQL Server database.
How can I quickly search? SQL Server Management Studio does not seem to do it. I use http://www.red-gate.com/products/SQL_Search/ but I'd like to find the "official" Microsoft solution to this. Is it in another product?
For example, when I do a mass search in visual studio, I would like to be able to also find something in all stored procedures.
Or maybe I'm not coding this the right way?
Carl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用:
...因为
SYSCOMMENTS
和INFORMATION_SCHEMA.routines
具有 nvarchar(4000) 列。因此,如果在位置 3998 处使用“name_of_object”,则不会找到它。SYSCOMMENTS
确实有多行,但INFORMATION_SCHEMA.routines
被截断。Use:
...because
SYSCOMMENTS
andINFORMATION_SCHEMA.routines
have nvarchar(4000) columns. So if "name_of_object" is used at position 3998, it won't be found.SYSCOMMENTS
does have multiple lines, butINFORMATION_SCHEMA.routines
truncates.参加聚会已经很晚了,但是...
您可以使用系统过程
sys.sp_depends
:结果是一个列出所有依赖于(即引用)
object_name
的数据库对象。每行包含引用对象的名称和类型,以及其他信息列,具体取决于object_name
的类型。注意:此过程已在 MS SQL Server 2008 中添加。
请参阅:MSDN 文档
文档说此过程可能会在未来版本中删除,并使用 sys.dm_sql_referencing_entities
Very late to the party, but...
You can use the system proc
sys.sp_depends
:The result is a table listing all of the database objects that depend on (i.e., reference)
object_name
. Each row contains the name and type of the referring object, along with other info columns, depending on the type ofobject_name
.Note: This proc was added in MS SQL Server 2008.
See: MSDN docs
The docs say that this proc may be removed in a future release, and to use sys.dm_sql_referencing_entities instead, but it's still alive and kicking in MS SQL 2017.
在 SQL 2008 中添加了 DMV(数据管理功能)sys.dm_sql_referencing_entities。它返回引用您传递给它的对象的任何对象。
In SQL 2008 the DMV (Data Management Function) sys.dm_sql_referencing_entities was added. Its returns any object that references the object you pass it.
使用未记录的 SQL sp:sp_msforeachdb
With the use of an undocumented SQL sp: sp_msforeachdb
在 SQL Server 2000 中,这里是一个可以在对象定义内部进行搜索的查询,支持最多 2000 个字符的搜索字符串。它使用 syscomments 表中的块。
In SQL Server 2000 here is a query that can search inside object definitions, supporting search strings of up to 2000 characters. It uses the chunks in the
syscomments
table.我使用此查询来查找存储过程中的所有表(或文本):
I use this query to look for all tables (or text) in the stored procedures:
当您右键单击对象资源管理器中的对象时,SQL Server Management Studio 具有查看依赖关系功能。这是您要找的吗?
SQL Server Management Studio has a View Dependencies feature when you right click on an object in the Object Explorer. Is this what you're looking for?
如果您想在 SSMS 中使用 OMG Ponies sql 作为键盘快捷键,请将以下 SP 添加到您的主数据库中。
然后,您只需将 dbo.SP_FindAllReferences 添加到键盘快捷键中,然后就可以在服务器上的任何数据库的上下文中使用它。
干杯!
注意:如果您使用的是 SQL Server 2005,则必须替换
为
If you want to use OMG Ponies sql as a keyboard shortcut in SSMS, add the following SP to your master db.
Then you just need to add dbo.SP_FindAllReferences to your keyboard shortcuts and then you can use it in the context of any DB on your server.
Cheers!
NB: If you are using SQL Server 2005 you will have to replace
with
我找到了这样的解决方案..
http://blog.sqlauthority.com/2012/12/02/sql-server-find-referenced-or-referencing-object-in-sql-server-using- sys-sql_表达式_依赖项/
I found a solution like this..
http://blog.sqlauthority.com/2012/12/02/sql-server-find-referenced-or-referencing-object-in-sql-server-using-sys-sql_expression_dependencies/
我不确定“官方微软”方式,但我过去使用过 SqlDigger 。这还不错。
如果您想在 VS 中执行此操作,那么您将需要项目中包含的所有过程的文本。
I'm not sure of 'official microsoft' way but I've used SqlDigger in the past. It's not bad.
If you want to do it in VS, then you will need the text of all your procs included in your project.