You are here: Re: Error message on using union statement with order by using reserved words « MsSQL Server « IT news, forums, messages
Re: Error message on using union statement with order by using reserved words

Posted by Roy Harvey (SQL Server MVP) on 10/30/07 11:45

On Tue, 30 Oct 2007 03:08:23 -0700, signon77 <signon77@yahoo.com>
wrote:

> ORDER BY
> ExternalId desc, Bookname asc, abs([Difference]) desc
>
>*****************************************************************************************************************
>
>I am trying to order the results of the UNIONs by the absolute value
>in the column Difference. 'Diffference' is a reserved word which is
>why it appears in square brackets. However every time I run this
>stored procedure I get the following error message:
>
>"ORDER BY items must appear in the select list if the statement
>contains a UNION operator"
>
>As 'Difference' is clearly being selected why am I getting this error
>message?

Because what you are trying to ORDER BY is an expression,
abs([Difference]), not a column in the result set. You can get away
with an expression with a single SELECT, but not when using UNION.

To get that order you would have to either add a column with that
expression to each SELECT in the UNION, or place the entire query
inside a derived table and ORDER BY in the outer query:

SELECT *
FROM (<your query here without ORDER BY>)
ORDER BY ExternalId desc, Bookname asc, abs([Difference]) desc

Roy Harvey
Beacon Falls, CT

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация