Dump list of full paths of parent/child type records in SQL Server 2008 R2

ぃ、小莉子 提交于 2019-12-06 09:27:19

Sounds like something that using a recursive CTE might be able to solve. A CTE (or common table expression) will allow you to create a table-like structure without having to create a table or view. CTEs also allow you to create recursive queries, which in your case can help tremendously. The structure allows you to join the CTE onto itself, lending to a 'cascading' type of result as you're intending to achieve here.

For example, you could do something like this:

WITH grouppaths (group_id, group_path) AS
(
    SELECT group_id, pathname
    FROM GroupPath
    WHERE parent_group_id IS NULL

    UNION ALL

    SELECT gp.group_id, gps.group_path + '/' + gp.pathname
    FROM GroupPath gp
    JOIN grouppaths gps ON gps.group_id = gp.parent_group_id
)

SELECT 
  group_id, group_path
FROM
  grouppaths

You can check this out here using SqlFiddle.

Microsoft provides information and samples about using CTEs here: http://msdn.microsoft.com/en-us/library/ms190766%28v=sql.105%29.aspx

More specifically, this link provides further information about recursive CTEs: http://msdn.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx

Based on the link posted by Matt Johnson, I was able to solve my challenge as such:

WITH group_paths (group_id, group_path)
AS
(
-- Anchor member definition
    SELECT g.group_id, cast('/'+g.name as varchar(max)) as group_path
    FROM dbo.blgroup AS g
    WHERE parent_group_id=0
    UNION ALL
-- Recursive member definition
    SELECT g.group_id, cast(group_path + '/' + g.name as varchar(max))
    FROM dbo.blgroup AS g
    INNER JOIN group_paths AS gp
        ON g.parent_group_id = gp.group_id
)
-- Statement that executes the CTE
SELECT group_id, group_path
FROM group_paths

The result looks like this, which is exactly what I needed:

group_id    group_path
----------- ----------------------------------------
1000001     /Servers
1000002     /Depot
1000003     /Jobs
1000004     /Component Templates
1000006     /System Packages
1000005     /Components
1000008     /Patch Repository
1000007     /Device
1000010     /Device/Imported
1000011     /Device/Provisioned
1000009     /Patch Repository/Patches By Subscription
2000148     /Components/Customers
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!