Translate

> > Select Random Record From DataBase (SQL)

Select Random Record From DataBase (SQL)

Posted on Monday, July 9, 2012 | No Comments

DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT

---- This will create a random number between 1 and 999
SET @Lower = 0 ---- The lowest random number
SET @Upper = (SELECT     COUNT(ItemID) AS Expr1 FROM dbo.basicSetupItem) ---- The highest random number
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--SELECT @Random;

WITH paging AS
(
  SELECT
  ROW_NUMBER() OVER (order by ItemID) AS rowid, *
  FROM vBasicSetupItem
)
select *
from paging
where rowid = @Random

Leave a Reply

Powered by Blogger.