您知道对于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