SELECT * INTO retains ORDER BY in SQL Server 2008 but not 2012

后端 未结 6 1208
星月不相逢
星月不相逢 2021-01-04 18:34

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

6条回答
  •  [愿得一人]
    2021-01-04 18:50

    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
    

提交回复
热议问题