I have this script to return all child and grandchildren tables with their level from a database . How do I change this if I only want to return the child and grandchildren tables of a particular table .
;WITH dependencies -- Get sp with dependenciesAS ( SELECT DISTINCT procs.NAME AS Tablename ,OBJDEP.NAME as Depends ,sch.name as schemaname FROM sysforeignkeys a INNER JOIN sysobjects OBJ ON a.fkeyid = OBJ.id INNER JOIN sysobjects OBJDEP ON a.rkeyid = OBJDEP.id inner join sys.objects procs on a.fkeyid = procs.object_id inner join sys.schemas sch on sch.schema_id=procs.schema_id where obj.type='U' and OBJDEP.type='u' ), no_dependencies AS ( SELECT obj.NAME AS Tablename ,sch.name as schemaname FROM sys.objects OBJ INNER JOIN sys.schemas sch on sch.schema_id=obj.schema_id WHERE NOT EXISTS ( SELECT 1 FROM dependencies k where obj.name=k.Tablename and sch.name=k.schemaname) and type='u' ), recursiv -- recursive CTE to get dependenciesAS ( SELECT Tablename AS [SP] , CAST('' AS VARCHAR(max)) AS DependsON , 0 AS LVL ,schemaname FROM no_dependencies UNION ALL SELECT d.Tablename AS [SP] ,CAST( d.Depends AS VARCHAR(max)) ,R.lvl + 1 AS LVL ,d.schemaname FROM dependencies d INNER JOIN recursiv r ON d.Depends = r.[SP] )SELECT * FROM RECURSIV ORDER BY LVL option (maxrecursion 0)