Changing newid() to newsequentialid() on an existing table

后端 未结 4 2076
鱼传尺愫
鱼传尺愫 2021-02-04 21:13

At the moment we have a number of tables that are using newid() on the primary key. This is causing large amounts of fragmentation. So I would like to change the column to use n

4条回答
  •  感情败类
    2021-02-04 21:33

    Thank you, yfeldblum! Your simple and concise explanation of COMB GUIDs really helped me out. I was actually looking at doing the reverse of this post: I had to get away from relying on newsequentialid() since I was trying to migrate a SQL Server 2012 db to Azure, and the newsequentialid() function is not supported there.

    I was able to change all of my table PK defaults to COMB GUIDs, with the following syntax:

    ALTER TABLE [dbo].[Company] 
    ADD  CONSTRAINT [DF__Company__Company_ID__72E6D332]  
        DEFAULT (CONVERT([uniqueidentifier],CONVERT([binary](10),newid(),0)+CONVERT([binary](6),getdate(),0),0)) FOR [CompanyId]
    GO
    

    My SQL2012 db is now happily living in the Azure cloud.

提交回复
热议问题