 <p>Try this, it's working for me.</p> <pre><code>UPDATE Team SET GroupID = CAST(RAND(CHECKSUM(NEWID()))*10000000 AS INT) WHERE GroupID IN (SELECT GroupID FROM Team GROUP BY GroupID HAVING COUNT(GroupID)&lt;4) </code></pre> <p>This code ensures random values are assigned for all GroupId's in Team table AND no GroupID would have an occurrence of more than 4 </p>