码迷,mamicode.com
首页 > 其他好文 > 详细

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

时间:2017-06-08 12:08:36      阅读:450      评论:0      收藏:0      [点我收藏+]

标签:nal   created   records   ons   bsp   order by   expr   .com   create   

https://stackoverflow.com/questions/30045871/sorting-the-view-based-on-frequency-in-sql-server

Just like in sub queries, you can‘t use ORDER BY in a view definition in sql server unless you also use TOP.

The reason for this is that Views are acted upon as if they where tables, and tables in sql server (in fact, in any relational database) are considered as not ordered sets.
Just like there is no meaning to the order of records stored in a table, there is also no meaning to the order of records fetched by a view.
You can use a dirty hack and write SELECT TOP 100 PERCENT ... and then use ORDER BY, but I doubt if it has any meaning at all.

Having said all that, you can of course use ORDER BY in any query that selects from a view.

 
https://stackoverflow.com/questions/1622878/why-use-select-top-100-percent
select top 100 percent * from vie_trn_47 order by createdon desc

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

标签:nal   created   records   ons   bsp   order by   expr   .com   create   

原文地址:http://www.cnblogs.com/chucklu/p/6961867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!