SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Combining union and union all


Combining union and union all

Author
Message
OzYbOi d(-_-)b
OzYbOi d(-_-)b
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1352 Visits: 778
thanks for the question and the input from everyone today.
Ken Wymore
Ken Wymore
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5097 Visits: 2377
Good question and discussion. I always enjoy order of precedence questions.
Keld Laursen (SEGES)
Keld Laursen (SEGES)
Old Hand
Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)

Group: General Forum Members
Points: 368 Visits: 202
Thank you for the question.
I deduced the result using operator precedence and parsing the input by hand (or head?).
Typed it into my local, friendly SSMS, and got the same result.
Although I hadn't really thought about it, the "operator precedence" combined with the "test data might thwart you" nature of this question really made the discussion pay off.

Keep your databases clean.
lightyun
lightyun
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 193
Really a nice question, make me learn more about this basic knowledge.

However, in real case, we really seldom use "Union" and "Union all" together just like the questions. always use any one of it to make the script consistence

anyway, now I am much more clearer about these two commands and the sequence effect
Tom Thomson
Tom Thomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14538 Visits: 12238
Nice question, and the explanation has provoked an interesting discussion.

Now there's maybe a problem with the explanation adopted as the result of that discussion: is it anywhere documented that UNION strings associate left to right unless overruled by parantheses, or could the optimiser decide (for whatever reason) to go right to left, or some arbitrary order? I guess this doesn't happen in existing versions (but haven't done extensive testing to verify that, it is purely a guess), but is that part of the T-SQL definition or something that a future quick fix could change? If it could change, then the right answer would presumably be "it depends what the optimiser chooses to do".

Tom

R.P.Rozema
R.P.Rozema
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1119 Visits: 1685
L' Eomot Inversé (3/9/2012)
Nice question, and the explanation has provoked an interesting discussion.

Now there's maybe a problem with the explanation adopted as the result of that discussion: is it anywhere documented that UNION strings associate left to right unless overruled by parantheses, or could the optimiser decide (for whatever reason) to go right to left, or some arbitrary order? I guess this doesn't happen in existing versions (but haven't done extensive testing to verify that, it is purely a guess), but is that part of the T-SQL definition or something that a future quick fix could change? If it could change, then the right answer would presumably be "it depends what the optimiser chooses to do".


I have not yet found any official docu stating that it is always evaluated left-to-right. So, I think you are right: "it may depend". BUT, if in a future version of SQL server this changes, then I am going to ask MS to re-evaluate a lot of change requests and bug reports on Connect that have been rejected because "the change could break existing applications" Cool. Waiting for the day! :-D



Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
Peter Trast
Peter Trast
Say Hey Kid
Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)Say Hey Kid (700 reputation)

Group: General Forum Members
Points: 700 Visits: 655
Koen Verbeeck (3/8/2012)
UNION queries are evaluated from left to right. If the last query contains duplicates and is preceded with UNION ALL, you will have duplicates in your result set.


Well put. Great question!

Peter Trast
Microsoft Certified ...(insert many literal strings here)
Microsoft Design Architect with Alexander Open Systems
ldorian81
ldorian81
SSC-Addicted
SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)SSC-Addicted (436 reputation)

Group: General Forum Members
Points: 436 Visits: 323
FYI. Doesn't work on SQL 2005

Error showing on line "Insert Into #t1(col) Values (1),(1);"

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.

I believe this function is not available until 2008!

Regards
ld

Stoke-on-Trent
United Kingdom

If at first you don't succeed, go to the pub and drink away your current thought plan.
R.P.Rozema
R.P.Rozema
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1119 Visits: 1685
Yes, you're right. Forgot about that. It's just for setting up the sample data, the union will still behave the same given the same data. i.e. if you insert the data using insert() select-statements you'll still be able to run it and get the same results.

As I said, it was tested on SQL 2008R2 only.



Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
Britt Cluff
Britt Cluff
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1803 Visits: 253
Good question. Thanks for submitting.

http://brittcluff.blogspot.com/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search