Thanks for the comment. Unfortunately, this results in:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'ORDER'.
Main Topics
Browse All TopicsI have written a valid query which contains a UNION ALL as well as an ORDER BY. I'll simplify it here as an example:
SELECT Surname, GivenName FROM Table 1
UNION ALL
SELECT Surname, GivenName FROM Table2
ORDER BY Surname
Let me say at this point: my query executes perfectly; all the relevant rows are retrieved and are nicely ORDERed BY my chosen column. However, I am trying to turn this into a VIEW using:
CREATE VIEW MyView AS
SELECT TOP 100 PERCENT Surname, GivenName FROM Table 1
UNION ALL
SELECT Surname, GivenName FROM Table2
ORDER BY Surname
It seems to be the UNION which is causing the problem:
CREATE VIEW MyView AS
SELECT TOP 100 PERCENT Surname, GivenName FROM Table 1
--UNION ALL
--SELECT Surname, GivenName FROM Table2
ORDER BY Surname
but this is not ordering correctly.
I even tried creating a second View which does a SELECT TOP 100 PERCENT * FROM MyView but the result is still not Ordered by the desired column properly. (The list is kinda alphabetically ordered - problem is it starts again at A's half way down - presumably cos of the UNION query.)
Any suggestions?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
OK,thanks.
I tried this but the ordering is still no good.
It is in order, but it gets to Z and then starts again - almost as though it applies the order to each table in the Union.
As a query, this returns the results perfectly.
BUT when this exact same query is put into a View (using Create View etc) the order is all over the place. Interestingly, I did spot this article:
http://www.microsoft.com/c
>>Never mind. The TOP allows the order by.<<
It does, but in SQL Server 2005 using 100 PERCENT does not guarantee the results are sorted :)
So please quit using an ORDER BY clause on a VIEW. While you may fool SQL Server 2005 by changing it to TOP 99.99999 PERCENT, that does not absolve it from being a lousy practice.
acperkins, are you saying that using TOP 99.99999 percent WOULD honour my ORDER BY clause in the View?
FYI: I have worked around this issue now anyway using a long-winded change within the client application (not SQL), so I am not looking to employ "TOP 99.99999 PERCENT"; I am just curious as to whether doing that causes the View to sort as stipulated in the ORDER BY clause.
Is it unfair of me to expect MS to give us a sortable View?
Did MS do this by choice to keep us developers busy? OR does the issue run deeper than that? :D
>> are you saying that using TOP 99.99999 percent WOULD honour my ORDER BY clause in the View?<<
I have no idea. Do a search on this website and you will see crappy solutions like that.
>>Is it unfair of me to expect MS to give us a sortable View?<<
Views were never intended to be sortable. The fact that people found cheesy workarounds, does not make it correct. So you can blame MS all you like, the fact of the matter the ones responsible are developers who relied on these tricks.
It makes no sense whatsoever to make Views sortable. Think about it, you create your complicated View and add an ORDER BY clause based on one column or two. Now you need the same columns in a different order, what are you going to do: Copy the View an modify? and so on. This is not MS Access, try and do it right the first time and you will not have to redo it.
Thanks Dad :D
Your comment was: "While you may fool SQL Server 2005 by changing it to TOP 99.99999 PERCENT...".
LIKE I SAID, I was not looking to employ this "TOP 99.99999 PERCENT" crap - I was just curious because I read your comment as implying that TOP 99.99999 PERCENT was somehow different from TOP 100 PERCENT in terms of its ability to sort a View.
I may not hold an EE-Genius ranking but even I can spot that "TOP 99.99999 PERCENT" is dodgy, which is why I didn't think of it. I am, however, into "learning" which is why I asked you to elaborate on your earlier comment.
Thanks.
Here is the logic behind why TOP 99.9999999 is supposed to "work". If you use TOP 100 the SQL Server engine knows that you want all the records and produces them all (minus the sort). If you use TOP 99.9999999 than it will attempt to only retrieve those records that comply.
As always with undocumented functionality your miles may vary.
Good luck.
Business Accounts
Answer for Membership
by: angelIIIPosted on 2008-12-16 at 06:02:05ID: 23183592
this will work better:
Select allOpen in new window