(あなたの例のように)事前に操作したいhierarchyid値がわかっている場合は、それを直接行うことができます。例:
-- Place 1st node between 2nd and 3rd
UPDATE yourTable SET node = CAST('/8/2.5/' AS hierarchyid) WHERE value = 36;
-- Move 3rd node to 1st
UPDATE yourTable SET node = CAST('/8/1/' AS hierarchyid) WHERE value = 34;
新しいhierarchyid値を動的に取得する必要がある場合は、GetDescendant ()関数とGetAncestor()関数を調べてください。これを使用すると、例は次のようになります。
DECLARE @Parent hierarchyid, @Child1 hierarchyid, @Child2 hierarchyid
-- Grab hierarchyids from 2nd and 3rd node
SELECT @Child1 = node FROM yourTable WHERE value = 38;
SELECT @Child2 = node FROM yourTable WHERE value = 34;
-- Get the parent hierarchyid
SELECT @Parent = @Child1.GetAncestor(1);
-- Update 1st node to end up between 2nd and 3rd
UPDATE yourTable SET node = @Parent.GetDescendant(@Child1, @Child2) WHERE value = 36;
-- Update 3rd node to end up before 2nd
UPDATE yourTable SET node = @Parent.GetDescendant(NULL, @Child1) WHERE value = 34;
この例では、hierarchyidは同じままではないことに注意してください。少なくとも1つは、その位置に「分数」を使用することになります(たとえば、「/8/3/」の代わりに「/8/ 2.5 /」)。