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