SQL order by and left outer join doesn't have correct order

一笑奈何 提交于 2019-12-06 05:50:15

Read Create a sorted view in SQL Server 2005 and SQL Server 2008 there is a hotfix and you HAVE to run in 2000 compatibility mode for it to work. Why not just do the order by when selecting from the view?

I would remove the ORDER BY clause from the view entirely. Instead, specify your order at the time you query the view.

MicSim

A comment in this blog entry makes it pretty clear.

Just once again the text from BOL:

"When ORDER BY is used in the definition of a view, inline function, derived table, or subquery, the clause is used only to determine the rows returned by the TOP clause. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself."

"Although the view definition contains an ORDER BY clause, that ORDER BY clause is used only to determine the rows returned by the TOP clause. When querying the view itself, SQL Server does not guarantee the results will be ordered, unless you specify so explicitly, as shown in the following query:

SELECT * FROM TopView
ORDER BY LastName

"

Most likely the two SQL Servers are slightly different versions. There's a known hotfix for this:

http://support.microsoft.com/kb/926292/

You should always "order" at the last possible point, since sorting is slow (N log N). When you perform a select on a view, you will usually include a where-clause. You only want to sort that result set.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!