English 中文(简体)
服务器: • 如何在自我推荐表中向父母提供所有子女记录
原标题:SQL Server: How to get all child records given a parent id in a self referencing table
  • 时间:2009-11-06 09:04:04
  •  标签:

然而,我有一张表格,其中提到自己,我需要能够从某个父母Id那里选择父母和所有子女记录。

我的表如下:

ID   | ParentID | Name         
-----------------------              
1      NULL       A
2      1          B-1
3      1          B-2
4      2          C-1
5      2          C-2

因此,就上述例子而言,我想通过价值1的表格,并查阅以上所有记录。

至今,我收到了以下重估表-价值-功能,但没有达到预期效果(只回到第一个记录)。

CREATE FUNCTION [dbo].[SelectBranches]
(   
    @id INT
    ,@parentId INT
)
RETURNS @branchTable TABLE
(
    ID INT
    ,ParentID INT
    ,Name INT
)
AS
BEGIN 

    IF @branchId IS NOT NULL BEGIN

        INSERT INTO @branchTable
        SELECT 
            ID
            ,ParentID
            ,Name
        FROM
            tblLinkAdvertiserCity
        WHERE
            ID = @id

    END

    INSERT INTO @branchTable
    SELECT
        br.ID
        ,br.ParentID
        ,br.Name
    FROM
        @branchTable b
    CROSS APPLY
        dbo.SelectBranches(NULL, b.ParentID) br

    RETURN
END
GO
最佳回答

你可以尝试这样做。

DECLARE @Table TABLE(
        ID INT,
        ParentID INT,
        NAME VARCHAR(20)
)

INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 1, NULL,  A 
INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 2, 1,  B-1 
INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 3, 1,  B-2 
INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 4, 2,  C-1 
INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 5, 2,  C-2 


DECLARE @ID INT

SELECT @ID = 2

;WITH ret AS(
        SELECT  *
        FROM    @Table
        WHERE   ID = @ID
        UNION ALL
        SELECT  t.*
        FROM    @Table t INNER JOIN
                ret r ON t.ParentID = r.ID
)

SELECT  *
FROM    ret
问题回答

在<代码>CTE上的回收成本为比值,因此,我写了这一功能,即利用复读功能,但速度大大加快了的复封。

CREATE FUNCTION [dbo].[Fn_GetSubCategories]
(
@p_ParentCategoryId INT
) RETURNS @ResultTable TABLE 
(   
    Id INT
)
AS
BEGIN
--Insert first level subcategories.
INSERT INTO @ResultTable 
SELECT Id FROM Category WHERE ParentCategoryId = @p_ParentCategoryId OR Id = @p_ParentCategoryId

DECLARE @Id INT
DECLARE @ParentCategory TABLE(Id INT)

DECLARE cur_categories CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY FOR 
SELECT Id FROM Category WHERE ParentCategoryId = @p_ParentCategoryId and Id != @p_ParentCategoryId
OPEN cur_categories
IF @@CURSOR_ROWS > 0
     BEGIN 
     FETCH NEXT FROM cur_categories INTO @Id
     WHILE @@FETCH_STATUS = 0
     BEGIN
        --Insert remaining level sub categories.
        IF EXISTS(SELECT 1 FROM Category WHERE ParentCategoryId = @Id AND Id != @Id)
        BEGIN
            INSERT INTO @ResultTable
            SELECT DISTINCT C.Id from Fn_GetSubCategories(@Id) C INNER JOIN @ResultTable R ON C.Id != R.Id
        END

     FETCH NEXT FROM cur_categories INTO @Id
     END

     --Delete duplicate records
     ;WITH CTE AS
     (SELECT *,ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Id) AS RN FROM @ResultTable)
     DELETE FROM CTE WHERE RN<>1

END
CLOSE cur_categories
DEALLOCATE cur_categories

RETURN

END

除非你使用Oracle,否则你的表格结构不适合所述问题。 你们试图做的是打碎等级(tra树结构)。

有一个条款,





相关问题
热门标签