Generate new GUID in SQL Server if one is not provided

后端 未结 2 775
一整个雨季
一整个雨季 2021-01-17 12:06

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

相关标签:
2条回答
  • 2021-01-17 12:43

    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.

    0 讨论(0)
  • 2021-01-17 12:48

    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..

    0 讨论(0)
提交回复
热议问题