0

多対多の親子関係テーブルがあります

parentid int not null
childid int not null

会社a、b、c、d、eがあるとしましょう

a
..b
....c
..c
d
..e
....b
......c

このクエリで上位の親を1つ返すようになりました

FUNCTION [dbo].[getRootCompagny] 
(
    @root int
)
RETURNS int
AS
BEGIN
DECLARE @parent int

SELECT @parent = companyparentid from companyrelation where companychildid = @root
if @parent is not null
   set @root = dbo.getRootCompagny(@parent)

RETURN @root
END

bを渡すと、

a
..b
....c
..c

私が書いたクエリは1つの最上位の親しか管理できないためです。最初のツリーのように、ツリー全体を取得できるようにするには、どのように修正しますか?

これが私のCTEです

    PROCEDURE [dbo].[GetCompanyRelation]
    @root int
AS
BEGIN
    SET NOCOUNT ON;

            set @root = dbo.getRootCompagny(@root)

    WITH cieCTE(CompanyChildid, CompanyParentid, depth, sortcol)
        AS
        (
            -- root member
            SELECT @root
                    , null 
                    , 0
                    , CAST(@root AS VARBINARY(900))


            UNION ALL

            -- recursive member
            SELECT R.CompanyChildid
                    , R.CompanyParentid
                    , C.depth+1
                    , CAST(sortcol + CAST(R.CompanyChildid AS BINARY(4)) AS VARBINARY(900))
            FROM CompanyRelation AS R JOIN cieCTE AS C ON R.CompanyParentid = C.CompanyChildid
        )

        -- outer query
        SELECT cieCTE.depth
                , cieCTE.CompanyChildid as ChildID
                , cieCTE.CompanyParentid as ParentId
                , company.[name] as [Name]
        FROM cieCTE inner join company on cieCTE.CompanyChildid = company.companyid
        ORDER BY sortcol
END

結局、上記のロジックで、私はa、dを取得する方法を取得しますか?

4

1 に答える 1

1

では、これを見ていただけませんか。インデントと完全なツリーパスを提供します

    DECLARE @Table TABLE(
        ID VARCHAR(10),
        ParentID VARCHAR(10)
)

--INSERT INTO @Table (ID,ParentID) SELECT 'a', NULL
INSERT INTO @Table (ID,ParentID) SELECT 'b', 'a'
INSERT INTO @Table (ID,ParentID) SELECT 'c', 'a'
INSERT INTO @Table (ID,ParentID) SELECT 'c', 'b'

--INSERT INTO @Table (ID,ParentID) SELECT 'd', NULL
INSERT INTO @Table (ID,ParentID) SELECT 'e', 'd'
INSERT INTO @Table (ID,ParentID) SELECT 'b', 'e'

DECLARE @Start VARCHAR(10)
SELECT @Start = 'e'

;WITH   roots AS (
        SELECT  *
        FROM    @Table
        WHERE   ID = @Start
        UNION   ALL
        SELECT  DISTINCT
                NULL,
                ParentID
        FROM    @Table
        WHERE   ParentID = @Start
        AND     ParentID NOT IN ( SELECT ID FROM @Table)
        UNION ALL
        SELECT  t.*
        FROM    @Table t INNER JOIN
                roots r ON t.ID = r.ParentID
)
,       layers AS(
        SELECT  ParentID AS ID,
                CAST(NULL AS VARCHAR(10)) AS ParentID,
                CAST('' AS VARCHAR(MAX)) AS DisplayDepth,
                CAST(ParentID + '\' AS VARCHAR(MAX)) AS LayerPath
        FROM    roots
        WHERE   ParentID NOT IN ( SELECT ID FROM @Table)
        UNION   ALL
        SELECT  t.*,
                DisplayDepth + '[-]',
                LayerPath + t.ID + '\'
        FROM    @Table t INNER JOIN
                layers l ON t.ParentID = l.ID
)
SELECT  *
FROM    layers
ORDER BY LayerPath

それはあなたが考えていたものですか?

于 2009-10-02T11:56:51.110 に答える