title: Insert main table's primary key into child table multiple times
body: <p>I have <code>MS Access 2007</code> database with the following schema:</p>
  
  <p>Main table <code>Object&lt; # Object_PK, ... &gt;</code></p>
  
  <p>Child table <code>Electric_Consumption&lt; $ Object_PK, # Electric_Consumption_PK, ... &gt;</code></p>
  
  <p>Child table <code>Water_Consumption&lt; $ Object_PK, # Water_Consumption_PK, ... &gt;</code></p>
  
  <p>Child table <code>Educational_Object&lt; $ Object_PK, # Educational_Object_PK, ... &gt;</code> which has child tables defined like this:</p>
  
  <p><code>School&lt; $ Educational_Object_PK, # School_PK, ... &gt;</code></p>
  
  <p><code>University&lt; $ Educational_Object_PK, # University_PK, ... &gt;</code></p>
  
  <p>Here is the picture that should make things clearer:</p>
  
  <p><img src="https://i.stack.imgur.com/eErtv.png" alt="enter image description here"></p>
  
  <p>I use <code>ADO</code> and <code>C++</code> to insert data.</p>
  
  <p>First I need to enter data for main table <code>Object</code>. I can successfully do that with <code>INSERT</code> query. </p>
  
  <p>My problem is following:</p>
  
  <p>After the above operation I need to insert <code>Object</code>'s <strong>primary key</strong> into child tables, since it is their foreign key.</p>
  
  <p>Allow me to describe exactly what I need so community can help me:</p>
  
  <p>As I said, first I insert data into main table <code>Object</code>. </p>
  
  <p>Then I need to insert data <strong>and <code>Object</code>'s primary key</strong> into child tables. </p>
  
  <p>Browsing through Internet I have found <code>@@IDENTITY</code> that might help me but I do not know if it works for my case.</p>
  
  <p>To make things harder, this will be done in <code>for loop</code> ( the value of the <code>Object_PK</code> is the same in every <code>INSERT</code> and is equal to the value of <strong>the last inserted record</strong> for the <code>Object</code> ) , something like this:</p>
  
  <pre><code>for ( //... ) 
     L"INSERT INTO Electric_Consumption ( Object_PK, field1, field2 ... ) 
         values ( Object_pk // should I use here @@IDENTITY ? );
  </code></pre>
  
  <p>Then the same thing should be repeated for tables <code>Water_Consumption</code> and <code>Educational_Object</code>.</p>
  
  <p>After I finish this, I need to add data in the <code>Educational_Object</code>'s child tables.</p>
  
  <p>The same as above, only instead of <code>Object_PK</code> I need to add <code>Educational_Object_PK</code>.</p>
  
  <p>Here is the pseudo-code to clarify things better:</p>
  
  <pre><code>L"INSERT INTO Object ( ... ) values ( ... ); //this is OK
  
  for ( ... )
      L" INSERT INTO Electric_Consumption ( Object_PK, ... ) 
          values ( Object_PK, ... )"; // should I use @@IDENTITY here
                                      // to get Object_PK ??
  for ( ... )
      L" INSERT INTO Water_Consumption ( Object_PK, ... ) 
          values ( Object_PK, ... )"; // should I use @@IDENTITY here
                                      // to get Object_PK ??
  for ( ... )
      L" INSERT INTO Educational_Object ( Object_PK, ... ) 
          values ( Object_PK, ... )"; //  should I use @@IDENTITY here
                                      // to get Object_PK ??
  for ( ... )
      L" INSERT INTO School ( Educational_Object_PK, ... ) 
          values ( Educational_Object_PK, ... )";// should I use @@IDENTITY here
                                      // to get Educational_Object_PK ??
  for ( ... )
      L" INSERT INTO University ( Educational_Object_PK, ... ) 
          values ( Educational_Object_PK, ... )";// should I use @@IDENTITY here
                                      // to get Educational_Object_PK ??
  </code></pre>
  
  <p>Can you please tell me which <code>SQL statement</code> to use for this, and demonstrate how to use it by providing a small pseudo code?</p>
  
  <p>I understand that my description of the problem might be confusing so if you need further clarification leave a comment and I will edit my post.</p>
  
  <p>Thank you.</p>
  
  <p>Best regards.</p>