# Thursday, October 21, 2004
« Automated Testing and Bugs | Main | Bummer »

In my Design section today, I ranted a bit about automated testing and a trend I have noticed to avoid directly accessing a database for that testing. It didn't start out to be a rant, but rather a tip about grabbing a random SQL record.

Using an ORDER BY NewID() clause, you can either randomly order your data, or grab a random single row. I use the single row method to grab data for testing so I am not always testing the same value, or occassionally the random order when I am searching for a particular set of data using a cursor for really complicated testing. Here's the usage:

Random order:

SELECT * FROM Authors ORDER BY NewID()

Single Random Record:

SELECT TOP 1 * FROM Authors ORDER BY NewID()