News for hackers. Stuff that matters.
Re:Nondeterministic Functions in MySQL (i.e. Rand) Can Surpr... by jcriddle4 on Thursday December 06, 2018 @03:09PM (#18621647)
For SQL Server it looks like an index seek on the following so it is evaluated once.

SELECT * FROM SomeTable WHERE ID = CAST(Rand() * 1000 as int)

In addition this query: SELECT TOP(100) rand() AS RandData from SomeTable

Returns: 0.940284064056996 0.940284064056996 0.940284064056996 . . . .

A old trick to actually get random data on SQL Server that I think has been replaced with newer functionality is:

SELECT ABS(CHECKSUM(NEWID())) FROM SomeTable

However if we move the NEWID.... stuff into the WHERE clause we end up switching to an index seek so back to being evaluated once.

There are no comments on this comment yet.