Select Random Record

Option 1 Pro: easy to understand Con: poor scalability, as rand() is gen'ed for each row, stored and indexed to perform the ORDER BY Option 2 Pro: easy to understand, Con: GUIDs > int Option 3 Pro: fast generator for # between 0 and recordcount Con: does not confirm that a record exists for the record/ID generated
Language:
T-SQL
Keywords:
Code Snippet

Option 1:

SELECT *
  FROM [tbl]
 ORDER BY RAND()

 

Option 2:

SELECT *
  FROM [tbl]
 ORDER BY NewID()

 

Option 3:

SELECT CAST(RAND() * COUNT(*) AS int)

 


Created 2012-01-25
comments powered by Disqus
Login