Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Granted, it is difficult to understand what you are trying to accomplish. If you are in fact trying to insert rows into two different tables, as marc_s stated, you must use two insert statements. </p> <p>However, judging from your sample, it may not be the case that you are trying to insert into two tables but rather use two tables to insert into a third table where you transpose the data. If that is the case, then you can do it in a single statement:</p> <pre><code>Insert MysteryTable( Id, fieldvalue ) Select 1, val1 From Table1 Union All Select 2, val2 From Table1 Union All Select 3, val3 From Table2 --assuming these come from Table2. Isn't clear in the OP Union All Select 4, val4 From Table2 --assuming these come from Table2. Isn't clear in the OP </code></pre> <p>Of course, if Table1 or Table2 has many rows, then you will obviously get many rows with the same Id value in your MysteryTable. </p> <p><strong>Update given change to OP</strong></p> <p>Given your clarification, what you seek can be done but requires two queries similar to the one above.</p> <pre><code>Insert Table1( Id, fieldvalue ) Select 1, val1 From SourceTable Union All Select 2, val2 From SourceTable Insert Table2( Id, fieldvalue ) Select 1, val3 From SourceTable Union All Select 2, val4 From SourceTable </code></pre> <p>Another variation which generates your id values would be:</p> <pre><code>With NumberedItems As ( Select val1 As val From SourceTable Union All Select val2 From SourceTable ) Insert Table1(id, fieldname) Select Row_Number() Over( Order By val ) As Num , val From SourceTable With NumberedItems As ( Select val3 As val From SourceTable Union All Select val4 From SourceTable ) Insert Table2(id, fieldname) Select Row_Number() Over( Order By val ) As Num , val From SourceTable </code></pre> <p>Btw, in the above example, I used <code>Union All</code>, however if you are trying to normalize the data, you might want to have distinct values. In that case, you would use <code>Union</code> instead of <code>Union All</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload