如何从SQL表中检索分层数据?

时间:2021-07-13 00:06:22

I have got the below stored procedure to return the list of Id, parentId and absoluteUrls which works fine:

我有以下存储过程来返回Id,parentId和absoluteUrls的列表工作正常:

ALTER PROCEDURE [dbo].[SearchDataManager.HierarchyById] 
    @currentId AS int   
AS
BEGIN
 DECLARE @id INT 
 DECLARE @parentId INT
 DECLARE @absoluteUrl NVARCHAR(1000)

 DECLARE @Hierarchy TABLE (Id int, ParentId int, AbsoluteUrl nvarchar(1000))

 WHILE @currentId != 0
 BEGIN

     SELECT @id = Id, @parentId = ParentId, @absoluteUrl = AbsoluteUrl
     FROM dbo.[SearchDataManager.NiceUrls]
     WHERE id = @currentId

     INSERT INTO @Hierarchy  VALUES (@id, @parentId, @absoluteUrl)

    SET @currentId = @parentId
 END

    SELECT * FROM @Hierarchy  
END

The "NiceUrls" table has Id and ParentId. parentId refers to a record in the same table.

“NiceUrls”表有Id和ParentId。 parentId引用同一个表中的记录。

it returns like:

它返回如下:

 ----------------------------------
    Id  | ParentId | AbsoluteUrl
    ----------------------------------
    294 | 5        | url1
    5   | 2        | url2
    2   | 0        | url3

The above code works fine using a WHILE loop and defining a Table variable but I'm just wondering is there any better way to retrieve hierarchy data from a table?

上面的代码使用WHILE循环和定义一个Table变量工作正常,但我只是想知道有没有更好的方法从表中检索层次结构数据?

The problem with the above code is maintainability. If I'd need to return 1 more column of the NiceUrls table then I'd have to define a new variable, add the column to the inline table, etc.

上述代码的问题是可维护性。如果我需要返回NiceUrls表的另一列,那么我必须定义一个新变量,将列添加到内联表中,等等。

Is there any better way to rewrite the sp?

有没有更好的方法来重写sp?

Thanks,

谢谢,

What's the

什么是

2 个解决方案

#1


17  

with Hierarchy (Id, ParentId, AbsoluteUrl, Level)
 AS
 (
    -- anchor member
     SELECT Id,
        ParentId,
        AbsoluteUrl,
        0 AS Level   
     FROM dbo.[NiceUrls]
     WHERE id = @currentId
     UNION ALL
     -- recursive members
     SELECT su.Id,
        su.ParentId,
        su.AbsoluteUrl,
        Level + 1 AS Level   
     FROM dbo.[NiceUrls] AS su
     INNER JOIN Hierarchy ON Hierarchy.ParentId = su.Id  
 )
 SELECT * FROM Hierarchy

#2


2  

Looks like you want all the records from the source table that are related to the original id.

看起来你想要源表中与原始id相关的所有记录。

1) Create a CTE that gives you all the ids (see the link Triple noted)

1)创建一个CTE,为您提供所有ID(请参阅三个注释链接)

2) Join this CTE to the original table

2)将此CTE加入原始表

#1


17  

with Hierarchy (Id, ParentId, AbsoluteUrl, Level)
 AS
 (
    -- anchor member
     SELECT Id,
        ParentId,
        AbsoluteUrl,
        0 AS Level   
     FROM dbo.[NiceUrls]
     WHERE id = @currentId
     UNION ALL
     -- recursive members
     SELECT su.Id,
        su.ParentId,
        su.AbsoluteUrl,
        Level + 1 AS Level   
     FROM dbo.[NiceUrls] AS su
     INNER JOIN Hierarchy ON Hierarchy.ParentId = su.Id  
 )
 SELECT * FROM Hierarchy

#2


2  

Looks like you want all the records from the source table that are related to the original id.

看起来你想要源表中与原始id相关的所有记录。

1) Create a CTE that gives you all the ids (see the link Triple noted)

1)创建一个CTE,为您提供所有ID(请参阅三个注释链接)

2) Join this CTE to the original table

2)将此CTE加入原始表