<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Views and ORDER BY</title><link>http://sqlblog.com/blogs/john_paul_cook/archive/2013/03/06/views-and-order-by.aspx</link><description>The general rule is that a view can't be created using a statement that contains an ORDER BY clause. There is a workaround that some people believe works. Similar to my recent post on using an ORDER BY with UNION ALL, the empirical facts don't always</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Views and ORDER BY</title><link>http://sqlblog.com/blogs/john_paul_cook/archive/2013/03/06/views-and-order-by.aspx#48088</link><pubDate>Thu, 07 Mar 2013 06:45:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48088</guid><dc:creator>arse</dc:creator><description>&lt;p&gt;try SELECT TOP 100 PERCENT instead&lt;/p&gt;
</description></item><item><title>re: Views and ORDER BY</title><link>http://sqlblog.com/blogs/john_paul_cook/archive/2013/03/06/views-and-order-by.aspx#48093</link><pubDate>Thu, 07 Mar 2013 11:31:10 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48093</guid><dc:creator>Dave Ballantyne</dc:creator><description>&lt;p&gt;Hi &lt;/p&gt;
&lt;p&gt;Top 100 percent gets optimized out and has no effect.&lt;/p&gt;
&lt;p&gt;BUT.....&lt;/p&gt;
&lt;p&gt;Ordering in a view is contradictory to RDBMS practice.&lt;/p&gt;
&lt;p&gt;The only place you should order is from a result set.&lt;/p&gt;
&lt;p&gt;A view is not a result set.&lt;/p&gt;
&lt;p&gt;You can select from a view to generate that result set and order that, but that is not the same as the view having order.&lt;/p&gt;
&lt;p&gt;Infact, i would go so far to say that even if you use TOP(X) order by , the only guarentee is that those rows returned are the TOP(X) and you cannot rely upon the order of those.&lt;/p&gt;
</description></item><item><title>re: Views and ORDER BY</title><link>http://sqlblog.com/blogs/john_paul_cook/archive/2013/03/06/views-and-order-by.aspx#48147</link><pubDate>Sat, 09 Mar 2013 15:26:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48147</guid><dc:creator>David Markle</dc:creator><description>&lt;p&gt;We had this issue with SQL 2005 as well, up until the first or second service pack. &amp;nbsp;(I'd like to point out that the &amp;quot;we&amp;quot; I refer to is the community, and not the software I've written :D )&lt;/p&gt;
&lt;p&gt;MS rightfully optimized out the TOP 100 PERCENT from views and tons of software which relied on this failed to work. &amp;nbsp;The root cause of all of this nonsense was that the drag n' drop view designer in SSMS actually GENERATED these view queries. &amp;nbsp;This pattern spread like a virus until it found its way into tons of software which promptly broke when the optimizer stopped ordering by views.&lt;/p&gt;
&lt;p&gt;What do they need to do? &amp;nbsp;IMO the best solution is to completely disallow SELECT TOP 100 PERCENT ... ORDER BY in view definitions altogether and throw an error when the view is defined. &amp;nbsp;Such a view definition could and should still compile in compatibility mode, but it would force software which relies on this pattern to break when views are moved over to a new compatibility mode.&lt;/p&gt;
&lt;p&gt;Of course, this wouldn't necessarily completely eliminate this problem, as you could always SELECT TOP &amp;lt;something bigger than the table will ever be&amp;gt; in a view to try to replicate the behavior, but those patterns are far, far less common than the SELECT TOP 100 PERCENT ... pattern.&lt;/p&gt;
</description></item><item><title>re: Views and ORDER BY</title><link>http://sqlblog.com/blogs/john_paul_cook/archive/2013/03/06/views-and-order-by.aspx#48157</link><pubDate>Sun, 10 Mar 2013 10:40:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48157</guid><dc:creator>z</dc:creator><description>&lt;p&gt;s&lt;/p&gt;
</description></item></channel></rss>