Set the controls to lightly toasted muffins... RSS 2.0
 Monday, May 15, 2006

Over the weekend I migrated various databases to SQL Server 2005 only to find that the hierarchical menus on the websites using the databases were incomplete and out of order.

After a few hours of head scratching I discovered some views along the lines of -

SELECT TOP 100 PERCENT col1, col2, col3
FROM sometable
ORDER BY col1, col2

The ORDER BY clause should only be used in views under certain conditions i.e. when using the TOP operator and the above view worked just fine under SQL Server 2000.

However in SQL Server 2005, the query optimiser optimises out TOP 100 PERCENT because it recognises that no rows are being eliminated from the resultset and because of this optimisation it also optimises out the the ORDER BY clause.

Monday, May 15, 2006 12:25:14 AM UTC  #    Comments [1] -
Techy
Monday, May 15, 2006 10:05:04 AM UTC
Even in SQL Server 2000 it was not guaranteed that the results of a view of this type would be correctly ordered - although they generally were. The ORDER BY clause only determines which results are returned by the TOP clause not the order of these results. You should always explicitly order the results of a view. More details here: http://www.developersdex.com/sql/message.asp?p=580&r=4984529&Page=1
sparkplug
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
Now Playing
Top Artists This Week
Fluff

Powered by FeedBurner
Categories
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Kevin Kenny
Sign In
Statistics
Total Posts: 194
This Year: 41
This Month: 0
This Week: 0
Comments: 101
All Content © 2008, Kevin Kenny
DasBlog theme 'Business' created by Christoph De Baene (delarou)