SQL Server 树查询

发布于 2024-10-17 16:26:16 字数 720 浏览 1 评论 0原文

我需要一些帮助是 MS SQL Server 查询。我算不上什么DBA。我有一个带有组织表的应用程序,该表由父子关系组成:

CREATE TABLE [dbo].[Organizations](
    [OrgPK] [int] IDENTITY(1,1) NOT NULL,
    [OrgParentFK] [int] NULL,
    [OrgName] [varchar](200) NOT NULL,
CONSTRAINT [PK__Organizations] PRIMARY KEY CLUSTERED

示例数据如下所示:

OrgPK,   OrgParentFK, OrgName
1,  0,  Corporate
2,  1,  Department A
3,  1,  Department B
4,  2,  Division 1
5,  2,  Division 2
6,  3,  Division 1
7,  6,  Section 1
8,  6,  Section 2

我正在尝试生成一个查询,该查询返回基于给定 OrgPK 的组织路径。例如,如果给出 OrgPK = 7,则查询将返回“Corporation/Department B/Division 1/Section 1”

如果给出 OrgPk = 5,则返回字符串将为“Corporation/Department A/Division 2”

感谢您的帮助。

I need some help is MS SQL Server Query. I’m not much of a DBA. I have an application with an Organization Table which is made up of a parent-child relationship:

CREATE TABLE [dbo].[Organizations](
    [OrgPK] [int] IDENTITY(1,1) NOT NULL,
    [OrgParentFK] [int] NULL,
    [OrgName] [varchar](200) NOT NULL,
CONSTRAINT [PK__Organizations] PRIMARY KEY CLUSTERED

Sample data looks like this:

OrgPK,   OrgParentFK, OrgName
1,  0,  Corporate
2,  1,  Department A
3,  1,  Department B
4,  2,  Division 1
5,  2,  Division 2
6,  3,  Division 1
7,  6,  Section 1
8,  6,  Section 2

I'm trying to generate a query that returns an org path based on a given OrgPK. Example if given OrgPK = 7 the query would return 'Corporation/Department B/Division 1/Section 1'

If give OrgPk = 5 the return string would be 'Corporation/Department A/Division 2'

Thank you for your assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-10-24 16:26:16
WITH  OrganizationsH (OrgParentFK, OrgPK, OrgName, level, Label) AS
(
    SELECT OrgParentFK, OrgPK, OrgName, 0, CAST(OrgName AS VARCHAR(MAX)) As Label
    FROM Organizations
    WHERE OrgParentFK IS NULL
    UNION ALL
    SELECT o.OrgParentFK, o.OrgPK, o.OrgName, level + 1,  CAST(h.Label + '/' + o.OrgName  VARCHAR(MAX)) As Label
    FROM Organizations o JOIN OrganizationsH h ON o.OrgParentFK = h.OrgPK
)

SELECT OrgParentFK, OrgPK, OrgName, level, Label
FROM OrganizationsH
WHERE OrgPK = 5

h/t 至 marc_s

WITH  OrganizationsH (OrgParentFK, OrgPK, OrgName, level, Label) AS
(
    SELECT OrgParentFK, OrgPK, OrgName, 0, CAST(OrgName AS VARCHAR(MAX)) As Label
    FROM Organizations
    WHERE OrgParentFK IS NULL
    UNION ALL
    SELECT o.OrgParentFK, o.OrgPK, o.OrgName, level + 1,  CAST(h.Label + '/' + o.OrgName  VARCHAR(MAX)) As Label
    FROM Organizations o JOIN OrganizationsH h ON o.OrgParentFK = h.OrgPK
)

SELECT OrgParentFK, OrgPK, OrgName, level, Label
FROM OrganizationsH
WHERE OrgPK = 5

h/t to marc_s

烈酒灼喉 2024-10-24 16:26:16

也可以通过创建标量值函数来解决:

-- SELECT [dbo].[ListTree](5)
CREATE FUNCTION [dbo].[ListTree](@OrgPK int)
RETURNS varchar(max)
AS
BEGIN
    declare @Tree varchar(MAX)
    set @Tree = ''

    while(exists(select * from dbo.Organizations where OrgPK=@OrgPK))
    begin
        select  @Tree=OrgName+'/'+@Tree,
                @OrgPK=OrgParentFK
        from    dbo.Organizations
        where   OrgPK=@OrgPK

    end
    return  left(@Tree,len(@Tree)-1)
END

It can also be solved by creating a scalar valued function:

-- SELECT [dbo].[ListTree](5)
CREATE FUNCTION [dbo].[ListTree](@OrgPK int)
RETURNS varchar(max)
AS
BEGIN
    declare @Tree varchar(MAX)
    set @Tree = ''

    while(exists(select * from dbo.Organizations where OrgPK=@OrgPK))
    begin
        select  @Tree=OrgName+'/'+@Tree,
                @OrgPK=OrgParentFK
        from    dbo.Organizations
        where   OrgPK=@OrgPK

    end
    return  left(@Tree,len(@Tree)-1)
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文