You are here: Re: Getting back set order from the IN param « MsSQL Server « IT news, forums, messages
Re: Getting back set order from the IN param

Posted by Dan Guzman on 11/05/06 14:17

In SQL, you *must* specify ORDER BY to return data in a particular sequence.
Consider results to be unordered unless you explicitly specify ORDER BY in
the outermost query. Even if a query returned results in your desired order
without ORDER BY, it would be due to chance rather than designed behavior.

Below is one method to accomplish your task.

SELECT STRINGTEXT, TOKENID
FROM WEBSTRINGS
JOIN (
SELECT 6 AS TOKENID, 1 AS SEQ
UNION ALL SELECT 20, 2
UNION ALL SELECT 234, 3
UNION ALL SELECT 19, 4
UNION ALL SELECT 32, 5
UNION ALL SELECT 4, 6
UNION ALL SELECT 800, 7
UNION ALL SELECT 177, 8) AS SELECT_SEQUENCE ON
SELECT_SEQUENCE.TOKENID = WEBSTRINGS.TOKENID
ORDER BY SELECT_SEQUENCE.SEQ


--
Hope this helps.

Dan Guzman
SQL Server MVP

"Yobbo" <info@SpamMeNot.co.uk> wrote in message
news:12krhpj4krke602@corp.supernews.com...
> Hi All
>
> My query is as follows:
>
> SELECT STRINGTEXT, TOKENID
> FROM WEBSTRINGS
> WHERE TOKENID IN (6,20,234,19,32,4,800,177)
>
> All I want is my resultset to come back in the order that I have defined
> in
> the IN clause, but unfortunately SQL is trying to be too helpful and sorts
> the numbers in the IN clause so that the resultset comes back with a
> TOKENID
> order of 4,6,19,20,32,177,234,800.
>
> I don't want this bloody order I want 6,20,234,19,32,4,800,177!!
>
> Sorry for my rant, but its got my hot under the collar.
>
> Is there anyway round this?
>
> Thanks
>
> Yobbo
>
>
>
>

 

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

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