I know this is an older question, but I also know I looked at it myself while coming up with my own solution.
Rather than using the deprecated sys.sql_dependencies
, you can use the sys.dm_sql_referencing_entities
table valued function.
The following query calls it recursively to trace down dependencies, showing each step in the dependency chain:
DECLARE @table varchar(max);
SET @table = 'schema.objectname';
;with
DepsOn As (
SELECT CAST(@table As varchar(max)) As parent
, CAST(l1.referencing_schema_name
+ '.'
+ l1.referencing_entity_name As varchar(max)) As child
, l1.referencing_class_desc As [description]
, 0 As Depth
FROM sys.dm_sql_referencing_entities(@table,'OBJECT') l1
UNION ALL
SELECT l2.child As parent
, cast(l2ca.referencing_schema_name
+ '.'
+ l2ca.referencing_entity_name As varchar(max)) As child
, l2ca.referencing_class_desc As [description]
, l2.Depth + 1 As Depth
FROM DepsOn l2
CROSS APPLY sys.dm_sql_referencing_entities(l2.child,'OBJECT') l2ca
)
SELECT *
FROM DepsOn