It is a bug.
Create Table #Test(Id Int NOT NULL)
INSERT Into #Test VALUES(1)
INSERT Into #Test VALUES(2)
SELECT @@VERSION
SELECT *
FROM (
SELECT [id]
,y.x As ryx
,RAND(y.x) As yx
FROM #Test, (SELECT CONVERT(varbinary,NEWID()) As x) y
) x
ORDER BY ryx --order by outside!
Example results:
id ryx
----------- -----------------------------------
1 0xC15FAED68C9A134882A2C977C46F1B8D --wrong order
2 0x532169D935535543BE0E0B24CA5D04FB --wrong order
In this query the order by
is clearly on the outside. It should not matter in what way the derived table x
is generated. The order by
must apply. The rows are returned in unsorted order.
Report it to Microsoft Connect. It is an optimizer bug.