dimanche 14 mars 2021

SQLite - Generate GUID/UUID on SELECT INTO statement

I am trying to script some test data for my application. The table I am trying to populate has a column of type string that holds a UUID. I need to write an insert statement that populates this column.

My current statement almost works, but it generates the same ID for all inserts. The generated column does not have to be a true UUID but does need to be unique in the table.

A simplified version of my statement looks like this:

SELECT 
null as id,
whi.externalId,
(select lower(hex( randomblob(4)) || '-' || hex( randomblob(2))
         || '-' || '4' || substr( hex( randomblob(2)), 2) || '-'
         || substr('AB89', 1 + (abs(random()) % 4) , 1)  ||
         substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6)))) as GUID
FROM WorkHerdInventories as whi

This results in the following:

enter image description here




Aucun commentaire:

Enregistrer un commentaire