title: Insert output IDs into another table
body: <p>I have a status table, and another table containing additional data. My object IDs are the PK in the status table, so I need to insert those into the additional data table for each new row.</p>
  
  <p>I need to insert a new row into my statusTable for each new listing, containing just constants.</p>
  
  <pre><code>declare @temp TABLE(listingID int)
  
  insert into statusTable(status, date)
    output Inserted.listingID into @temp
  select 1, getdate()
  from anotherImportedTable
  </code></pre>
  
  <p>This gets me enough new listing IDs to use.</p>
  
  <p>I now need to insert the actual listing data into another table, and map each row to one of those listingIDs - </p>
  
  <pre><code>insert into listingExtraData(listingID, data)
  select t.listingID, a.data
  from @temp t, anotherImportedTable a
  </code></pre>
  
  <p>Now this obviously doesn't work, because otherDataTable and the IDs in @temp are unrelated... so I get far too many rows inserted.</p>
  
  <p>How can I insert each row from <code>anotherImportedTable</code> into <code>listingExtraData</code> along with a unique newly created <code>listingID</code>? could I possibly trigger some more sql at the point I do the <code>output</code> in the first block of sql?</p>
  
  <p><strong>edit</strong>: thanks for the input so far, here's what the tables look like:</p>
  
  <p><em>anotherImportedTable</em>:</p>
  
  <pre><code>data
  </code></pre>
  
  <p><em>statusTable</em>:</p>
  
  <pre><code>listingID (pk), status, date
  </code></pre>
  
  <p><em>listingExtraData</em>:</p>
  
  <pre><code>data, listingID
  </code></pre>
  
  <p>You see that I only want to create one entry into <code>statusTable</code> per row in <code>anotherImportedTable</code>,  then put one <code>listingID</code> with a row from <code>anotherImportedTable</code> into <code>listingExtraData</code>... I'm thinking that I might have to resort to a cursor perhaps?</p>