title: Hierarchical Database Select / Insert Statement (SQL Server)
body: <p>I have recently stumbled upon a problem with selecting relationship details from a 1 table and inserting into another table, i hope someone can help.</p>
  
  <p>I have a table structure as follows: </p>
  
  <pre><code>ID (PK)   Name       ParentID&lt;br&gt;
  1         Myname     0&lt;br&gt;
  2         nametwo    1&lt;br&gt;
  3         namethree  2
  </code></pre>
  
  <p>e.g</p>
  
  <p>This is the table i need to select from and get all the relationship data. As there could be unlimited number of sub links (is there a function i can create for this to create the loop ?)</p>
  
  <p>Then once i have all the data i need to insert into another table and the ID's will now have to change as the id's must go in order (e.g. i cannot have id "2" be a sub of 3 for example), i am hoping i can use the same function for selecting to do the inserting.</p>