Default Sort Order for Query Selection in SQL Server 2005 and SQL Server 2012

Is there a difference between the default sort order for the query of choice in SQL Server 2005 and SQL Server 2012?

I have a table variable without a primary key. When I execute a select query on a table variable in SQL Server 2005, the records are selected and displayed in alphabetical order according to one of the columns. In SQL Server 2012, records are displayed in the same order as in the parent table.

+5
source share
2 answers

There is no default sort order. If you do not specify it in the ORDER BY , there is no guarantee that the result will be returned in the same way all the time.

You may notice that the result is an order using PK or a clustered index, but this will not always be the case.


You can read the following:

Without ORDER BY, there is no default sort order from Alexander Kuznetsov

+6
source

MSDN says:

Indicates that the values ​​in the specified column should be sorted in ascending or descending order. ASC sorts from the lowest value to the highest value. DESC sorts from the highest value to the lowest value. ASC is the default sort order. Zero values ​​are considered the lowest values.

So, in the ORDER BY clause, if you do not provide a sort order, then it will be sorted in the default ASCENDING order in 2005 and 2012, respectively.

However, if you do not specify an ORDER BY clause, the sort order is undefined and undefined.

0
source

Source: https://habr.com/ru/post/1245287/


All Articles