Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The pseudo is too cryptic (reduced?) It is very unlikely to get 2 columns per cross join of 2 tables in each of the union components</p> <pre><code>INSERT INTO T1(A,B) (SELECT * FROM E,R) UNION (SELECT * FROM Z,X) </code></pre> <p>Note: If you have ANY order by clause at all, it <em>must</em> be at the end of the union</p> <pre><code>INSERT T1(A,B) SELECT P,Q FROM E,R UNION SELECT R,S FROM Z,X </code></pre> <p><em>@updated based on error text "Server: Msg 104, Level 15, State 1, Line 1 ORDER BY items must appear in the select list if the statement contains a UNION operator"</em></p> <p>This occurs when you have a union that attempts to perform ORDER BY on a column that does not appear in the result. Consider a normal ORDER BY involving non-selected columns</p> <pre><code>select top 10 name from syscolumns order by xtype </code></pre> <p>The rows are consistent and the query can be satisfied. However, if you did</p> <pre><code>select top 10 name from syscolumns where xtype &gt; 50 union all select top 10 name from syscolumns where xtype &lt; 50 order by xtype </code></pre> <p>EVEN IF xtype exists in both parts of the UNION, but the time it gets presented to ORDER BY (which works at the END over the entire result set), the column is not there. You would have to rewrite it (if you didn't want to show <code>xtype</code>) as</p> <pre><code>select name from ( select top 10 name, xtype from syscolumns where xtype &gt; 50 union all select top 10 name, xtype from syscolumns where xtype &lt; 50 ) x order by xtype </code></pre> <p>Hope that helps</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. 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