Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 919
Robyn Dias Avatar asked Dec 08 '22 20:12

Robyn Dias


1 Answers

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();
like image 87
Rahul Avatar answered Dec 14 '22 22:12

Rahul