I will edit some fake data in my database. But if I will make random numbers for each row and column, it is not so random as I want it. The result can you see on image below.
Here is my code I use:
WITH x AS
(
SELECT mintwee, mineen, nul, pluseen, plustwee
FROM Topic
)
SELECT mintwee = CAST(RAND()*100 AS INT),
mineen = CAST(RAND()*100 AS INT),
nul = CAST(RAND()*100 AS INT),
pluseen = CAST(RAND()*100 AS INT),
plustwee = CAST(RAND()*100 AS INT)
FROM x
and this:
WITH x AS
(
SELECT
mintwee = CAST(RAND()*100 AS INT),
mineen = CAST(RAND()*100 AS INT),
nul = CAST(RAND()*100 AS INT),
pluseen = CAST(RAND()*100 AS INT),
plustwee = CAST(RAND()*100 AS INT)
FROM Topic
)
SELECT mintwee, mineen, nul, pluseen, plustwee
FROM x ;
But both has the same result. I know, I must use an UPDATE
statement, but this is only for try if the result is good. After it, I am going to use the UPDATE
statement.
I use SQL server with an express 2014 version.
Best Answer
Since there is no correlation between the table rows and the RAND function, the expression is evaluated only once in the query.
One method to generated random values is with the NEWID() function. The example below generates random integer values between 0-99 range. The modulo value can be adjusted for a different range and you can use this basic technique to generate random values for dates, character, etc.
EDIT:
Per Aaron's comment, the expression can be simplified if you need only 32-bit integers. Additional examples: