Personal tools
You are here: Home SQL Server How To's How To Retrieve Rows Randomically in SQL Server
Navigation
Log in


Forgot your password?
 
Document Actions

How To Retrieve Rows Randomically in SQL Server

-- First, let's create a table to performance some tests

CREATE TABLE #tbl (SomeColumn INT)

INSERT INTO #tbl (SomeColumn) VALUES (0)

INSERTINTO #tbl (SomeColumn) VALUES (1)

INSERT INTO #tbl (SomeColumn) VALUES (2)

INSERT INTO #tbl (SomeColumn) VALUES (3)

INSERT INTO #tbl (SomeColumn) VALUES (4)

INSERT INTO #tbl (SomeColumn) VALUES (5)

INSERT INTO #tbl (SomeColumn) VALUES (6)

INSERT INTO #tbl (SomeColumn) VALUES (7)

INSERT INTO #tbl (SomeColumn) VALUES (8)

INSERT INTO #tbl (SomeColumn) VALUES (9)

-- You can use RAND()

-- But according with the Books OnLine, there's a chance to not work randomically

-- This code didn't work with me

SELECT SomeColumn FROM #tbl ORDER BY Rand()

-- Another option is to use NewID() function (It's more secure)

SELECT SomeColumn FROM #tbl ORDER BY NewID()

-- Drop the table

DROP TABLE #tbl

Security Awareness
Would you like your company to implement gamification into your security awareness program?





Polls