Xerratus
Happily stressed out, since 1974


 
Thursday, January 10, 2008
<< Uninstalled Vista and reinstalled XP
A better way to create a dynamic SQL stored procedure >>

Nice little trick if you want to return results sorted randomly from a SQL 2000/2005 database:

SELECT * FROM Customers ORDER BY newid()

So say you want to return 10 random customers from the database, you'd modify the statement as such:

SELECT TOP 10 * FROM Customers ORDER BY newid()

For those who don't know, newid() generates a new guid, just as Guid.NewGuid() does in .NET.  By ordering a uniquely generated guid, we're guaranteed random results.