Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL Server Possible Order By Bug

$
0
0

Hello, 

We noticed some odd behaviour of the ORDER BY clause inside the following pieces of code: 

-- Batch 1:

DECLARE@iINT = 0

SELECT@i= @i+a

  FROM (SELECT 1aUNION ALLSELECT 2 UNIONALLSELECT 3)x

  ORDERBY a

SELECT@i

GO

-- Batch 2:

DECLARE@iINT = 0

SELECT@i= @i+a

  FROM (SELECT 1aUNION ALLSELECT 2 UNIONALLSELECT 3)x

  ORDERBY 1

SELECT@i

Does anyone know why in Batch 2 example only the last value of the UNION is added to the @i variable ? 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>