Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate guid for a table without a loop

I have a table with 100 records with no PK.

I need to add random GUID to it. Before:

    First Name   Last Name   GUID
      John         Smith
      Alex         Smith
etc

After:

    First Name   Last Name   GUID
      John         Smith    34234234gyerfw
      Alex         Smith    werwer32r23r
etc

Currently I can do it by:

Creating an identity column with values, then make a while loop and generate newid(). Any option on how to do this without a loop?

like image 398
user194076 Avatar asked Dec 01 '25 05:12

user194076


1 Answers

Just add the empty column and then do an UPDATE.

ALTER TABLE YourTable
ADD GUID UNIQUEIDENTIFIER

UPDATE YourTable
SET GUID = NEWID()
like image 129
Lamak Avatar answered Dec 03 '25 22:12

Lamak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!