I'm having a problem with random values being generated for each row in a result set in SQL Server 2008. I found a similar question here, but upon implementing the proposed answer, I saw the same problem as before. When running the query I have provided below, it seems that the same values will sometimes show up in consecutive rows, even though I'm calling for a new NEWID()
with each row.
DECLARE @Id int = 0
DECLARE @Counter int = 1
DECLARE @Value int
CREATE TABLE #Table1
(
id int identity(1,1)
,Value int
)
WHILE @Counter < 100000
BEGIN
INSERT INTO #Table1 (Value)
SELECT CAST(RAND(CHECKSUM(NEWID())) * 100000 as INT)
SET @Counter += 1
END
SET @Counter = 0
WHILE @Counter < 5
BEGIN
SELECT
@Value = T.Value
,@Id = T.id
FROM #Table1 T
WHERE T.id = CAST(RAND(CHECKSUM(NEWID())) * 100000 as INT) + 1 + @Counter
IF @Id <> 0
SELECT @Value AS Value ,@Id as ID
SET @Counter += 1
END
DROP TABLE #Table1
If I change the INT
to a BIGINT
, as suggested in the link I provided, nothing is solved, so I don't believe that it's an "overflow" issue.