Thursday, April 14, 2011

TSQL – Find Object Dependencies


Yeah!!.. it is getting pretty much interesting here.. the following query we can use to find the Tables used in the stored procedures (‘Object on which the Stored Procedure depends’).  It is also can be viewed by right click the object and click the ‘View Dependency’ , it will give results.. but the TSQL as follows:
SELECT b.name,a.referencing_id, a.referenced_database_name, a.referenced_entity_name
FROM sys.sql_expression_dependencies a
JOIN sys.procedures b on a.referencing_id = b.object_id

But…regardless of whether it is stored procedure or View,  if we want to find the dependency of all the object in the database use the following script.

SELECT b.name,referencing_id,referenced_database_name, referenced_entity_name
FROM sys.sql_expression_dependencies a
JOIN sys.objects b on a.referencing_id = b.object_id

…Hope It Saves time.  :)

No comments:

Post a Comment