Want to script all objects which depend on a SQL Server table

老子叫甜甜 提交于 2019-12-05 02:48:39

问题


The 'View Dependecies' shows all the objects which depend on a table in SQL Server. Now how do I use SSMS to script all these objects in one command? Is there a free tool that does this?


回答1:


First you can try this link Understanding SQL Dependencies

Second, you have multiple options to check the Dependencies

using sql_expression_dependencies table, to see the dependency of X on Y, run the following query.

SELECT * 
FROM sys.sql_expression_dependencies 
WHERE referencing_id = OBJECT_ID('X')
    AND referenced_id = OBJECT_ID('Y')
    AND referenced_schema_name = 'dbo'
    AND referenced_entity_name = 'Y'
    AND referenced_database_name IS NULL
    AND referenced_server_name IS NULL;

using the syscomments table, SQL Server's syscomments table stores the original SQL definition statement for every view, rule, default, trigger, CHECK and DEFAULT constraint, and stored procedure in your database. That's a lot of information! You can query this table to list dependent objects using a SQL statement in the following form

SELECT *
FROM syscomments 
INNER JOIN sysobjects sysobj ON syscomments.id = sysobj.id
WHERE charindex('your object to check', text) > 0 

using the sp_depends stored procedure, wich displays information about database object dependencies, such as: the views and procedures that depend on a table or view, and the tables and views that are depended on by the view or procedure

EXEC sp_depends @objname = N'your object to check'



回答2:


SSMS relyies on SMO to extract the dependencies. The DependencyWalker class, namely. You can wrap this in your code, and the SMO also has the posibility to script out object definitions using the Scripter class (which again is what SSMS uses to script object definitions too).



来源:https://stackoverflow.com/questions/1750519/want-to-script-all-objects-which-depend-on-a-sql-server-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!