SQLTeam.com | Weblogs | Forums

WHILE select query that randomizes rows with each call

I have a select query within a WHILE loop.

With each loop, the query is called. Each time it's called (i.e., with each loop), I want the records to be ordered randomly. I don't care if it's "truly" random or pseudo random. I just want each call of the query to have a different order of records.

Here is the basic version of my WHILE loop and embedded query. RAND() and NEWID() returns records in the same order with each call. I want each call to return records in a different (random or pseudorandom) order.

DECLARE @cycles INTEGER = 3, @i INTEGER = 1
WHILE @i <= @cycles
BEGIN

SELECT
	*		
FROM
	MYTABLE
ORDER BY
	RAND()

END

Use ORDER BY NEWID()

Thank you. Got NEWID() to actually work.