title: Inserting many rows in treelike structure in SQL SERVER 2005
body: <p>I have a few tables in SQL that are pretty much like this</p>
  
  <pre><code>A            B           C
  ID_A         ID_B        ID_C
  Name         Name        Name
               ID_A        ID_B
  </code></pre>
  
  <p>As you can see, A is linked to B and B to C. Those are basically tables that contains data models. Now, I would need to be able to create date based on those tables. For example, if I have the following datas</p>
  
  <pre><code>A                    B                        C
  1    Name1           1  SubName1   1          1  SubSubName1  1
  2    Name2           2  SubName2   1          2  SubSubName2  1
                       3  SubName3   2          3  SubSubName3  2
                                                4  SubSubName4  3
                                                5  SubSubName5  3
  </code></pre>
  
  <p>I would like to copy the 'content' of those tables in others tables. Of course, the auto numeric key that is generated when inserting into the new tables are diffirent that those one and I would like to be able to keep track so that I can copy the entire thing. The structure of the recipient table contains more information that those, but it's mainly dates and other stuff that are easy to get for me.</p>
  
  <p>I would need to this entirely in TRANSACT-SQL (with built-in function if needed). Is this possible and can anyone give me a short example. I manage to do it for one level, but I get confused for the rest.</p>
  
  <p>thanks</p>
  
  <p>EDIT : The info above is just an example, because my actual diagram looks more like this</p>
  
  <p>Model tables :</p>
  
  <pre><code>Processes -- (1-N) Steps -- (1-N) Task             -- (0-N) TaskCheckList
                           -- (0-N) StepsCheckLists
  </code></pre>
  
  <p>Where as the table I need to fill looks like this</p>
  
  <pre><code>Client -- (0-N) Sequence -- (1-N) ClientProcesses -- (1-N) ClientSteps -- (1-N)ClientTasks -- (0-N) ClientTaskCheckList
                                                                         -- (0-N)ClientStepCheckLists
  </code></pre>
  
  <p>The Client already exists and when I need to run the script, I create one sequence, which will contains all processes, which will contains its steps, taks, etc...</p>