SQL Server UNION - What is the default ORDER BY Behaviour

前端 未结 6 1990
生来不讨喜
生来不讨喜 2020-11-27 20:38

If I have a few UNION Statements as a contrived example:

SELECT * FROM xxx WHERE z = 1
UNION 
SELECT * FROM xxx WHERE z = 2
UNION
SELECT * FROM xxx WHERE z =         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 21:26

    There is no default order.

    Without an Order By clause the order returned is undefined. That means SQL Server can bring them back in any order it likes.

    EDIT: Based on what I have seen, without an Order By, the order that the results come back in depends on the query plan. So if there is an index that it is using, the result may come back in that order but again there is no guarantee.

提交回复
热议问题