Reparenting a Tree branch is a PITA using the HierarchyID functions. This reparents a branch by reformating the string representation of the HierarchyID. Supports moving the branch to the root node.
I use an ID as the primary key so the proc expects the ID's of the node records
DECLARE
@NodeID INT,
@NewParentID int
SET @NodeID = 1074
SET @NewParentID = 1073
DECLARE
@OldParent VARCHAR(100),
@NewParent VARCHAR(100),
@NodeKey VARCHAR(100)
SELECT
@OldParent = NodeKey.GetAncestor(1).ToString(),
@NodeKey = NodeKey.ToString()
FROM ReportSetNode
WHERE NodeID = @NodeID
SELECT @NewParent = Nodekey.ToString() FROM ReportSetNode WHERE NodeID = @NewParentID ;
IF @NewParent IS NULL SET @NewParent = '/'
UPDATE vwReportSetNode SET
NodeKey = @NewParent + RIGHT(Nodekey.ToString(),LEN(NodeKey.ToString()) - LEN(@OldParent))
WHERE NodeKey.IsDescendantOf(@NodeKey) = 1
Started my programming life writing Excel 1.0 macros, God what a long time ago.
Now I'm a dotnet developer, I get to influence direction, play with new toys, build stuff, life is wonderful.
Greatest buzz you can get, walk past a row of desks and see your application running on all of them (and getting paid).
Greatest irritant, pouring 12 months of knowledge and experience into an empty head only to have it leave.
And now I'm retired, no deadlines, no meetings, no managers (except the ONE) and no users!