1 Replies - 1639 Views - Last Post: 29 January 2008 - 09:50 AM

#1 Trogdor   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Random row in MS SQLServer

Posted 30 November 2006 - 07:18 AM

Description: The way to get a random row is different in every database. The random() function will not work, as the outcome will allways be the same (has to do with precompiling and cashing afaik) This query does work however. Note that on very large tables it can be a bit slow, since a new guid is generated for every row!
SELECT TOP 1 ID, * 
FROM YourTable 
WHERE OtherConditions 
ORDER BY NEWID()

Is This A Good Question/Topic? 0
  • +

Replies To: Random row in MS SQLServer

#2 supersloth   User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4676
  • View blog
  • Posts: 28,499
  • Joined: 21-March 01

Re: Random row in MS SQLServer

Posted 29 January 2008 - 09:50 AM

I used this today and it worked great, thanks for uploading!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1