3

我指的是Bill Karwin 的演示文稿,以实现一个有助于我管理层次结构的闭包表。不幸的是,演示文稿没有显示我如何插入/更新Level幻灯片 67 中提到的列;这将非常有用。我一直在考虑,但我无法提出可以测试的具体内容。这是我到目前为止得到的:

create procedure USP_OrganizationUnitHierarchy_AddChild 
    @ParentId UNIQUEIDENTIFIER,
    @NewChildId UNIQUEIDENTIFIER
AS
BEGIN
    INSERT INTO [OrganizationUnitHierarchy]
    (
        [AncestorId],
        [DescendantId],
        [Level]
    )
    SELECT [AncestorId], @NewChildId, (here I need to get the count of ancestors that lead to the currently being selected ancestor through-out the tree)
    FROM [OrganizationUnitHierarchy]
    WHERE [DescendantId] = @ParentId
    UNION ALL SELECT @NewChildId, @NewChildId
END
go 

我不确定我该怎么做。有任何想法吗?

4

1 回答 1

5

您知道对于 Parent = self 您有 Level = 0 并且当您从祖先复制路径时,您只是将 Level 增加 1:

create procedure USP_OrganizationUnitHierarchy_AddChild 
    @ParentId UNIQUEIDENTIFIER,
    @NewChildId UNIQUEIDENTIFIER
AS
BEGIN
    INSERT INTO [OrganizationUnitHierarchy]
    (
        [AncestorId],
        [DescendantId],
        [Level]
    )
    SELECT [AncestorId], @NewChildId, [Level] + 1
    FROM [OrganizationUnitHierarchy]
    WHERE [DescendantId] = @ParentId
    UNION ALL
    SELECT @NewChildId, @NewChildId, 0
END
于 2013-09-04T20:06:40.610 回答