Random Rows From SQL Server
|
|
Let's say you have a table of x number of rows. You need to pick a random number (say 10) of rows from this table for your application. How do you do this in SQL Server??
authors is a table that exists in the pubs database that ships
with SQL Server and we select 10 random rows from it. The key to the randomness is the usage of the NEWID()
function. This function returns a new uniqueidentifier (also called GUID) for each call and this is
what causes the randomness.
Note that this method works only on machines running Windows 2K or higher.
|
| Home |