Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:20:47+00:00 2026-05-25T19:20:47+00:00

I am importing data from one database to another. The hierarchyid column must also

  • 0

I am importing data from one database to another. The hierarchyid column must also be ported over with the relationships intact.

If the target table is empty, it’s easy. When the target contains rows, the hierarchy as it is in the source table will not be valid in the target table

The easiest would be to increment all values in the hierarchy field with the value of the first ported row’s primary key.

So if a source row’s hierarchyid was '/1/12/13/' and the next available id in target before importing was 101, the hierarchy should change so each value is incremented by 100: '/101/112/113/'

I have read Updating “Hierarchyid” in SQL Server, but do not see how it can be applied to my problem.

How would I go about incrementing each number in each hierarchyid of each row by 1 set value, i.e. increment all numbers in the hierarchy field by 100?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:20:48+00:00Added an answer on May 25, 2026 at 7:20 pm

    Look at this solution (explanations can be found bellow):

    DECLARE @Source TABLE
    (
        ID HIERARCHYID PRIMARY KEY
        ,Name NVARCHAR(50) NOT NULL
    );
    
    DECLARE @Target TABLE
    (
        ID HIERARCHYID PRIMARY KEY
        ,Name NVARCHAR(50) NOT NULL
        ,OldID HIERARCHYID
    );
    
    INSERT  @Source 
    VALUES  
            ('/1/','a'), ('/1/1/','aa'), ('/1/2/','ab'), ('/1/3/','ac')
            ,('/2/','b')
            ,('/3/','c'), ('/3/3/','cc')
            ,('/4/','d'), ('/4/1/','da'), ('/4/2/','db'), ('/4/2/1/','dba'), ('/4/2/1/5/','dbaf');
    
    DECLARE @LastTargetRootNodeAsInteger INT;       
    SELECT  @LastTargetRootNodeAsInteger = REPLACE( MAX( a.ID.GetAncestor( a.ID.GetLevel()-1 ) ).ToString(), '/', '')
    FROM    @Target a;
    
    WITH CteUpdate
    AS
    (
    SELECT  a.ID 
            ,a.Name
            ,a.ID.GetAncestor( a.ID.GetLevel()-1 ) AS OldRootID
            ,CONVERT(HIERARCHYID,'/'+CONVERT(VARCHAR(256),@LastTargetRootNodeAsInteger+DENSE_RANK()OVER(ORDER BY a.ID.GetAncestor( a.ID.GetLevel()-1 )))+'/') NewRootID
    FROM    @Source a
    )
    INSERT  @Target(ID, Name, OldID)
    SELECT  a.ID.GetReparentedValue(a.OldRootID, a.NewRootID), a.Name, a.ID
    FROM    CteUpdate a;
    
    SELECT  *
            ,t.ID.ToString() AS CurrentNodeToString
            ,t.OldID.ToString() AS OldNodeToString
    FROM    @Target t
    ORDER BY t.ID;
    

    Explanation:

    1. First step suppose to find last root node (@LastTargetRootNodeAsInteger) from the target table.
    2. Then, for every ID from source table we get the root node (old root node: a.ID.GetAncestor( a.ID.GetLevel()-1 ) ).
    3. For every old root node we get the new root node ('/'+...+@LastTargetRootNodeAsInteger+DENSE_RANK()OVER(ORDER BY old_root_node)+'/').
    4. And finally, we will insert the rows with the new root node (a.ID.GetReparentedValue(old_root,new_root)).

    To see how we get the new IDs, you can execute this query:

    SELECT  *
            ,c.ID.GetReparentedValue(c.OldRootNode,c.NewRootNode).ToString() AS NewCurrentNode
    FROM
    (
            SELECT  *
                    ,'/'+CONVERT(VARCHAR(256),@LastTargetRootNodeAsInteger+DENSE_RANK() OVER(ORDER BY b.OldRootNode))+'/' AS NewRootNode
            FROM
            (
                    SELECT  *
                            ,a.ID.ToString() AS OldCurrentNode
                            ,a.ID.GetAncestor( a.ID.GetLevel()-1 ).ToString() AS OldRootNode
                    FROM    @Source a
            ) b
    ) c
    

    Results:

    results

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm importing data from one database into another. There are about 5,000 records
I'm importing a database dump from one Oracle 10g installation into another. The source
I have a method that transfers data from one database to another and in
In SSIS,I had tried to deploy a package, which is for importing data from
I'm importing some data from a CSV file, and numbers that are larger than
So, there have been several posts here about importing and saving data from an
I am importing massive amounts of data from Excel that have various table layouts.
My application is storing location data from GPS inputs. When importing a GPX file,
I'm importing from a CSV and getting data roughly in the format { 'Field1'
I have an existing SQL Server database, where I store data from large specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.