您知道对于Parent = self,您的Level = 0,并且当您从祖先复制路径时,您只是将Level增加1:
create procedure USP_OrganizationUnitHierarchy_AddChild @ParentId UNIQUEIDENTIFIER, @NewChildId UNIQUEIDENTIFIERASBEGIN INSERT INTO [OrganizationUnitHierarchy] ( [AncestorId], [DescendantId], [Level] ) SELECt [AncestorId], @NewChildId, [Level] + 1 FROM [OrganizationUnitHierarchy] WHERe [DescendantId] = @ParentId UNIOn ALL SELECT @NewChildId, @NewChildId, 0END



