首页 > 其他 > 详细

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 11:49:13      阅读:971      评论:0      收藏:0      [点我收藏+]

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.

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!