sqlsql-server-2012newid

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!


Solution

  • 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();