Want to script all objects which depend on a SQL Server table
The 'V开发者_如何学编程iew 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?
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'
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).
精彩评论