Instead of rand()
, use newid()
, which is recalculated for each row in the result. The usual way is to use the modulo of the checksum. Note that checksum(newid())
can produce -2,147,483,648 and cause integer overflow on abs()
, so we need to use modulo on the checksum return value before converting it to absolute value.
UPDATE CattleProds
SET SheepTherapy = abs(checksum(NewId()) % 10000)
WHERE SheepTherapy IS NULL
This generates a random number between 0 and 9999.