Al Lelopath
Al Lelopath

Reputation: 6778

Populate all columns with random int

I'm trying to populate 3 columns all rows with some random ints.
The code below has an error

Incorrect syntax near "@Upper. Expecting ( or Select

What am I doing wrong?

---- Create the variables for the random number generation
DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT

---- Create a random number between 1 and 111
SET @Lower = 1   -- The lowest random number
SET @Upper = 111 -- The highest random number

UPDATE [tblProject]
SET 
       [ContractorID1] = @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) -- error
      ,[ContractorID2] = @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) -- error
      ,[ContractorID3] = @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) -- error

Upvotes: 0

Views: 50

Answers (1)

pmbAustin
pmbAustin

Reputation: 3970

Take out the "@Random = " in your update statement. It doesn't serve any purpose. You aren't using that variable at all, so you have no need to even declare it. Just do direct assignment.

And as you've discovered, the RAND() function will return the same value for the same row. If you need something different, there are other solutions.

Upvotes: 1

Related Questions