The recursive CTE is producing duplicated results for nested dependencies in a hierarchy format. I want to have a field that spells out the path to each option.
I have tried rewriting this statement from a top down and bottom up approach but I have not been able to eliminate dependencies that don't actually exist. For example, if I have base A, child B, and grandchild C, I want A, A\\B, and A\\B\\C - but not A\\C.
WITH rCTE
(
[Level], -- Dimension count
[RootSchema], -- Child table db schema
[RootID], -- Child table ID, sys.foreign_keys.parent_object_ID
[RTableName], -- Name of the child table
[ParentSchema], -- Parent table db schema
[ParentID], -- Parent table IDsys.foreign_keys.referenced_object_ID
[PTableName], -- Name of the parent table
[Path] -- The path to the item
) AS (
SELECT
1 as [Level],
object_schema_name(f.parent_object_id),
f.parent_object_id as [RootID],
object_name(f.parent_object_id) as [RTableName],
OBJECT_SCHEMA_NAME(f.referenced_object_ID),
CONVERT(int,null) as [ParentID],
object_name(referenced_object_id) as [PTableName],
CONVERT(varchar(150),object_name(f.referenced_object_id) --+ '\' + ISNULL(object_name(f.parent_object_id),'') -- Troubleshooting
) as [Path]
FROM
sys.foreign_keys f join sys.tables t on t.object_id = f.parent_object_id
--WHERE NOT EXISTS
--( Select 1
-- from sys.foreign_keys ff
-- where f.parent_object_id = ff.referenced_object_id
--)
UNION ALL
SELECT
[Level]+1,
object_schema_name(f.parent_object_id),
f.parent_object_id,
object_name(f.parent_object_id),
OBJECT_SCHEMA_NAME(f.referenced_object_ID),
f.referenced_object_id
,object_name(f.referenced_object_id)
,CAST(r.[Path] + '\' + r.[RTableName] as varchar(150))
from sys.foreign_keys f join rCTE r on f.referenced_object_id = r.rootID
--where f.parent_object_id <> r.ParentID
)
select distinct x.[level] -- change
--,r.ParentSchema
,r.[PTableName]
,r.[RTableName]
,r.[Path]
from rCTE r join
(
select
[ptableName], max([Level]) as [Level]
from rCTE
GROUP BY [pTableName]
) x on x.pTableName = r.pTableName
ORDER BY [Path]
--select distinct * from rcte
I referenced many sites but this was the best one, and this image taken from there demonstrates the kind of path I am talking about. Picture of sample code results from a similar goal
I reached out to Kris Wenzel of the blog I had originally linked in my question after I received an e-mail from his mailing list that suggested sending him challenges you are encountering. He was able to provide a solution to the posed inquiry, which I've included below. This will map out a bottom-up approach to hierarchies based solely on the foreign keys within the database.
;
WITH cte_FKtable(Parent
, FKConstraintName
, Child)
AS (SELECT PO.name AS ParentTable
, FK.name
, RO.name AS ChildTable
FROM sys.foreign_keys AS FK
INNER JOIN sys.objects AS RO ON RO.object_id = FK.referenced_object_id
INNER JOIN sys.objects AS PO ON PO.object_id = FK.parent_object_id),
cte_allTable(parent
, FKConstraintName
, child)
AS (SELECT Parent
, FKConstraintName
, Child
FROM cte_FKtable
UNION
SELECT O.name
, NULL
, NULL
FROM sys.objects AS O
WHERE type = 'U' AND -- user table
NOT EXISTS
(
SELECT 1
FROM cte_FKtable
WHERE( cte_FKtable.parent = o.name OR
cte_FKtable.child = o.name
)
)),
cte_tree(name
, description
, level
, sort)
AS (SELECT DISTINCT
parent
, CAST(parent AS varchar(1024))
, 1
, CAST(parent AS varchar(1024))
FROM cte_allTable AS a
WHERE a.parent NOT IN
(
SELECT child
FROM cte_allTable
WHERE child IS NOT NULL
) OR
a.child IS NULL
UNION ALL
SELECT FK.child
, CAST(REPLICATE('|---', cte_tree.level) + FK.child AS varchar(1024))
, cte_tree.level + 1
, CAST(cte_tree.sort + '\' + FK.child AS varchar(1024))
FROM cte_tree
INNER JOIN cte_FKtable AS FK ON cte_tree.name = FK.parent)
SELECT DISTINCT
name
, description
, level
, sort
FROM cte_tree
ORDER BY sort;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.