A colleague of mine discovered a behaviour in SQL Server which I was unaware of.
CREATE VIEW dbo.vRandNumber AS
SELECT RAND() as RandNumber
GO
CREATE FUNCTION dbo.RandNumber() RETURNS float AS
RETURN (SELECT RandNumber FROM vRandNumber)
GO
DECLARE @mytable TABLE (id INT)
INSERT INTO @mytable SELECT 1
INSERT INTO @mytable SELECT 2
INSERT INTO @mytable SELECT 3
SELECT *, dbo.RandNumber() FROM @mytable
This seems to be the quickest way of generating a 'random' value for each record in a data set. But I'm not completely sure if it's a result of documented behaviour, or taking advantage of a bizarre convergance of coincidences.
Would you use something like this?
EDIT
This isn't a question about the merits of the RAND() function itself, but the use of the UDF/VIEW combination to force it to recalculate on every row. (Using just RAND() in the final query, instead of dbo.RandNumber(), would give the same value for every record.)
Also, the point is for the value to be different every time you look at it. So enabling random selection of records, for example.
EDIT
For SQL Server 2000+.