SQL - NEWID() producing the same guid on an update query

12.1k views Asked by At

In a table I have created a new column that's going to hold a randomized alphanumeric string. I've been using the NEWID() function. From my understanding, NEWID should just create a new guid per row but all my rows end up with the same guid.

My code is:

DECLARE @random_guid
SET @random_guid = NEWID()
UPDATE this_table
SET random_column = @random_guid

Thanks in advance for any help!

1

There are 1 answers

2
Rahul On BEST ANSWER

That's probably cause you are setting the variable first and using it. Rather directly use the function like

UPDATE this_table
SET random_column = NEWID();