11

Execute the following SQL in 2008 and 2012. When executed in 2008, the returned result is in its correct sort order. In 2012, the sortorder is not retained.

Is this a known change? Is there a work-around for 2012 to retain the sort order?

CREATE TABLE #MyTable(Name VARCHAR(50), SortOrder INT)
INSERT INTO #MyTable SELECT 'b', 2 UNION ALL SELECT 'c', 3 UNION ALL SELECT 'a', 1 UNION ALL SELECT 'e', 5 UNION ALL SELECT 'd', 4

SELECT * INTO #Result FROM #MyTable ORDER BY SortOrder

SELECT * FROM #Result

DROP TABLE #MyTable
DROP TABLE #Result
1
  • A table in SQL Server never has any implicit order - if you need a specific order, you must supply an ORDER BY clause to your SELECT statement to get it.
    – marc_s
    May 20, 2013 at 18:53

6 Answers 6

18

How can you tell what the order is inside a table by using select * from #result? There is no guarantee as to the order in a select query.

However, the results are different on SQL Fiddle. If you want to guarantee that the results are the same, then add a primary key. Then the insertion order is guaranteed:

CREATE TABLE MyTable(Name VARCHAR(50), SortOrder INT)
INSERT INTO MyTable SELECT 'b', 2 UNION ALL SELECT 'c', 3 UNION ALL SELECT 'a', 1 UNION ALL SELECT 'e', 5 UNION ALL SELECT 'd', 4


select top 0 * into result from MyTable;

alter table Result add id int identity(1, 1) primary key;

insert into Result(name, sortorder)
    SELECT * FROM MyTable
    ORDER BY SortOrder;

I still abhor doing select * from Result after this. But yes, it does return them in the correct order in both SQL Server 2008 and 2012. Not only that, but because SQL Server guarantees that primary keys are inserted in the proper order, the records are even guaranteed to be in the correct order in this case.

BUT . . . just because the records are in a particular order on the pages doesn't mean they will be retrieved in that order with no order by clause.

1
  • 1
    Thank you - creating the identity column did it. The only thing I did differently was created the #Result table first with CREATE TABLE, since the INSERT truncated the data otherwise.
    – Rivka
    May 20, 2013 at 19:31
10

When using ORDER BY with an INSERT, it has never been guaranteed to do anything other than control the order of the identity column if present.

Prior to SQL Server 2012, the optimizer always produced a plan as if an identity column existed and thus appears to order correctly. SQL Server 2012 correctly does not assume an identity column exists, and only orders if the table actually has an identity column.

So you can resolve this issue by adding an Identity column to your temp result table.

However, you really should just add an ORDER BY clause to your SELECT statement? SELECT statements without an ORDER BY have never been guaranteed to return the results in any specific order. Always add the ORDER BY clause to ensure you receive the results the way you expect.

2
  • Thanks for the explaining the reasoning behind. BTW, adding the ORDER BY to the select isn't ideal for me, since there are multiple "sets" of data with different sets of sort order. So the ORDER BY wouldn't be accurate, as there can be multiple records with "1" as the sort order...
    – Rivka
    May 20, 2013 at 19:33
  • 1
    The reason for the difference in behaviour is not because SQL Server always assumed there was an IDENTITY column. It is because in versions prior to 2012 plans generated needed to be suitable whether or not run with a non zero SET ROWCOUNT option. 2012 caches plans with non zero row count separately so there is no need to pollute the main plan with these operators. May 21, 2013 at 6:46
2

First, thanks sgeddes for the explanation, it helped a lot. The thing about defining a table variable or creating a temp table is you have to define it, and if you are going to go through the work of defining it, you might as well do the insert the correct way:

INSERT INTO #Result (col1, col2...)
SELECT Col1, Col2... FROM #MyTable....

In my case, the ORDER BY in the INSERT was dynamic so when I called "SELECT * FROM #Result", the ORDER BY was unknown. My solution was to add a ROW_NUMBER column that I could hardcode into the SELECT when I was getting the data. Yea, I still have to include an ORDER BY, but at least it's static. Here's what I did:

--Insert
SELECT ROW_NUMBER() OVER (ORDER BY T.SortOrder ASC) AS RowNum, T.*  
INTO #Result 
FROM (SELECT * FROM #MyTable ...) AS T;

--Get data out
SELECT * FROM #Result ORDER BY RowNum;

Hope this helps.

1

Workaround : You could add a SET ROWCOUNT before this type of query, then put if back to zero after to reset it, it works. This will force SQL to keep the order in your query.

SET ROWCOUNT 1000000000

CREATE TABLE #MyTable(Name VARCHAR(50), SortOrder INT)
INSERT INTO #MyTable SELECT 'b', 2 UNION ALL SELECT 'c', 3 UNION ALL SELECT 'a', 1 UNION ALL SELECT 'e', 5 UNION ALL SELECT 'd', 4

SELECT * INTO #Result FROM #MyTable ORDER BY SortOrder

SELECT * FROM #Result

SET ROWCOUNT 0

DROP TABLE #MyTable
DROP TABLE #Result
0
0

If you have different sorted results when querying each database, your collation is probably different between the two.

Try explicitly setting the collation in your query and see if your results are returned in the same order in both databases, e.g.

SELECT * FROM #Result ORDER BY C1 COLLATE Latin1_General_CS_AS
0

You must to create ROW_NUMBER() order by column you want to order. Order by directly in the select, is ignored when insert is executed.

CREATE TABLE #MyTable(Name VARCHAR(50), SortOrder INT)

INSERT INTO #MyTable 
SELECT 'b', 2 
UNION ALL SELECT 'c', 3 
UNION ALL SELECT 'a', 1 
UNION ALL SELECT 'e', 5 
UNION ALL SELECT 'd', 4

SELECT  Name,
        ROW_NUMBER() OVER (ORDER BY MyTable.SortOrder) AS SortOrder 
INTO #Result 
FROM #MyTable AS MyTable
ORDER BY SortOrder

SELECT * FROM #Result

DROP TABLE #MyTable
DROP TABLE #Result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.