I am updating my database to have a column for a GUID for each record in a table.
This table is populated by several different sources and some of them will not pr
You can set the default value for the column to NEWID()
. Then a GUID will be created and assigned only if one hasn't already been.
DO NOT I REPEAT DO NOT PLACE A DEFAULT COLUMN OF NEWID()
. If you do the GUID you get will be completely random and if you have any sort of indexing on this column it will be useless. If you are going to do this you want to set the column default to be NEWSEQUENTIALID ( )
Kimberly Tripp has a Great Article and another Great Article on this subject.
To implement NEWSEQUENTIALID ( )
as a default:
CREATE TABLE foo
(unq_id UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID(),
cola varchar(10));
Full SQL Fiddle example..