Friday, June 8, 2007
It seems every 3 years or so I need to be slapped upside the head and reminded that in SQL Server...

record order in a result set is not guaranteed unless you use ORDER BY

So I don't have to be hit in the head again, I'm writing it down.

The issue is that:
frequently the results ARE returned in order of entry
and
frequently developers start assuming that will always be the case
and
frequently developers build code based on that assumption.

Months go by.

Then a subtle bug occasionally appears and someone spends the better part of an evening trying to replicate the bug, and finally generates mostly, but not entirely correct results.  By all appearances nothing is missing, but the code is angry when prodded just so.  Running the underlying stored procedure in Query Analyzer reveals that the records being returned are slightly out of order. The relevant SELECT statement lacks an ORDER BY clause.  The clause is added.  The records are returned in the expected order.  The code is happy.  The developer can go to sleep.

Friday, June 8, 2007 9:40:53 AM (Eastern Standard Time, UTC-05:00)   #     Comments [1]  | 
Thursday, May 5, 2011 11:18:05 AM (Eastern Standard Time, UTC-05:00)
And I thought I was the sensible one. Thanks for setting me strahigt.
Comments are closed.

Theme design by Dean Fiala

Pick a theme: