Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating hierarchyID as part of bulk insert

Status
Not open for further replies.

rotaxmax12

Technical User
Mar 22, 2011
5
US
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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top