rotaxmax12
Technical User
I have to migrate data from one table to a master table that uses HierarchyID data types. I can establish the parent, but I am not sure of the SQL syntax to be able to find the next node based on the parent.
I used this to initially seed the database:
WITH updateHID AS
(SELECT struct.Id AS TranID
, struct.Parent_Parent AS TranParent
, struct.Parent_Hid AS oldHID
, CAST(CONVERT(varchar(20),tranLine.Parent_Hid.ToString()) + CONVERT(varchar(20),row_number() over(partition by struct.Parent_Parent ORDER BY struct.Parent_Parent)) + '/' as hierarchyid) AS newHID
FROM dbo.Asset AS struct
JOIN dbo.Asset AS tranLine ON struct.Parent_Parent = tranLine.Id
WHERE tranLine.AssetType = 'D76AF6C7-9A0F-4A7C-BE0C-3A429976C2F7'
AND struct.AssetType IN ('924A8376-35B6-4CA9-B326-FCB6D31E5B7F','3C8C74E5-1492-497E-AA1D-9920E424F765') )
UPDATE updateHID
SET oldHID = newHID
I just need to know how to have this script/stored procedure going forward that it will get the max node for each parent.
Thanks
I used this to initially seed the database:
WITH updateHID AS
(SELECT struct.Id AS TranID
, struct.Parent_Parent AS TranParent
, struct.Parent_Hid AS oldHID
, CAST(CONVERT(varchar(20),tranLine.Parent_Hid.ToString()) + CONVERT(varchar(20),row_number() over(partition by struct.Parent_Parent ORDER BY struct.Parent_Parent)) + '/' as hierarchyid) AS newHID
FROM dbo.Asset AS struct
JOIN dbo.Asset AS tranLine ON struct.Parent_Parent = tranLine.Id
WHERE tranLine.AssetType = 'D76AF6C7-9A0F-4A7C-BE0C-3A429976C2F7'
AND struct.AssetType IN ('924A8376-35B6-4CA9-B326-FCB6D31E5B7F','3C8C74E5-1492-497E-AA1D-9920E424F765') )
UPDATE updateHID
SET oldHID = newHID
I just need to know how to have this script/stored procedure going forward that it will get the max node for each parent.
Thanks