如何修复递归CTE以基于sys.foreign_keys建立表依赖关系?

时间:2019-04-25 13:43:01

标签: sql sql-server common-table-expression

递归CTE为层次结构格式的嵌套依赖项生成重复的结果。我想要一个字段,说明每个选项的路径。

我试图用自上而下和自下而上的方法重写此语句,但是我无法消除实际上不存在的依赖项。 例如,如果我有基数A,子B和孙C,则需要A,A \ B和A \ B \ C-但不希望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

我引用了许多站点,但是this是最好的站点,从那里拍摄的这张照片演示了我所谈论的路径。 Picture of sample code results from a similar goal

1 个答案:

答案 0 :(得分:0)

在我收到他的邮件列表中的一封电子邮件,向他发送您遇到的挑战后,我联系了我最初在问题中链接的博客的Kris Wenzel。他能够为提出的查询提供解决方案,我将在下面进行介绍。这将为仅基于数据库中的外键的自下而上的层次结构方法映射。

;
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;