且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 视图中是否支持 ORDER BY?

更新时间:2022-11-25 09:55:57

SQL Server 开发人员假设任何设置操作都可能改变记录的顺序,因此没有必要使用 ORDERBY 在中间集定义中,它只在最终语句中有意义.

SQL Server developers assume that any set operation may change the order of the records so there is no point to use ORDER BY in the intermediate set definitions and it only makes sense in the final statements.

视图可能用于连接或其他使它们的订单无效的操作.

The views may be used in joins or other operations which invalidates their orders.

由于您不能单独使用视图,因此 i.e.你不写 vMyView,而是写 SELECT * FROM vMyView,尽管视图本身就是一个 SELECT,你可以如果您需要订单,(并且应该)将 ORDER BY 子句附加到 SELECT 语句.

Since you cannot use the view by itself, i. e. you don't write vMyView, you rather write SELECT * FROM vMyView, despite the fact that the view is a SELECT per se, you can (and should) append the ORDER BY clause to the SELECT statement as well if you need an order.

这是一个非常合理的假设,实际上它使代码更加清晰.

This is quite a sane assumption and in fact it makes the code more clear.