DESCRIPTION
Wrote this stored procedure below which RECURSIVELY lists all dependent child objects and child children and child ... etc. Input parameter can be saved in Proc, User Function, View. You can easily modify it to get a unique list of columns 5, no matter what level the Object called, and how deep and what the object is.
COLUMN
- UsedByObjectId - the parent object that uses the dependent object
- UsedByObjectName - name of the parent object
- UsedByObjectType - type of the parent object (P, V, FN)
- DependentObjectId - a child that uses the parent
- DependentObjectName - name of the child
- DependentObjectType - type of dependent child (P, V, FN, U)
- Level . How deep is the nested recursive level in which this object is used.
CODE
--========================================================================= --========================================================================= --== utlGetAllDependentObjectsRecursive - Uses recursive common table --== expression to recursively get all the dependent objects as well --== as the child objects and child child objects of a --== Stored Procedure or View or Function. can be easily modified to --== include all other types of Objects --========================================================================= --========================================================================= CREATE PROCEDURE utlGetAllDependentObjectsRecursive ( -- Supports Stored Proc, View, User Function, User Table @PARAM_OBJECT_NAME VARCHAR(500) ) AS BEGIN WITH CTE_DependentObjects AS ( SELECT DISTINCT b.object_id AS UsedByObjectId, b.name AS UsedByObjectName, b.type AS UsedByObjectType, c.object_id AS DependentObjectId, c.name AS DependentObjectName , c.type AS DependenObjectType FROM sys.sysdepends a INNER JOIN sys.objects b ON a.id = b.object_id INNER JOIN sys.objects c ON a.depid = c.object_id WHERE b.type IN ('P','V', 'FN') AND c.type IN ('U', 'P', 'V', 'FN') ), CTE_DependentObjects2 AS ( SELECT UsedByObjectId, UsedByObjectName, UsedByObjectType, DependentObjectId, DependentObjectName, DependenObjectType, 1 AS Level FROM CTE_DependentObjects a WHERE a.UsedByObjectName = @PARAM_OBJECT_NAME UNION ALL SELECT a.UsedByObjectId, a.UsedByObjectName, a.UsedByObjectType, a.DependentObjectId, a.DependentObjectName, a.DependenObjectType, (b.Level + 1) AS Level FROM CTE_DependentObjects a INNER JOIN CTE_DependentObjects2 b ON a.UsedByObjectName = b.DependentObjectName ) SELECT DISTINCT * FROM CTE_DependentObjects2 ORDER BY Level, DependentObjectName END
source share