自动增量uniqueidentifier [英] Autoincrement uniqueidentifier

查看:175
本文介绍了自动增量uniqueidentifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想使用uniqueidentifier与身份相似的方式。我不想插入值,它应该只是自动插入值,每行不同的值。
我无法对类型为uniqueidentifier的列设置自动增量(属性autoincrement设置为false,不可编辑)。

解决方案

或更好:使用 newsequentialid()作为UNIQUEIDENITIFER列的默认值。这将给你一个有序的系列GUID。

  CREATE TABLE dbo.YourTable 
(SerialID UNIQUEIDENTIFIER
CONSTRAINT DF_SerialID DEFAULT newsequentialid(),
....(其他列)......

问题是:newsequentialid只是可用作列默认值 - 您不能将其称为函数或任何东西。

更新:似乎是SQL Server Management Studio中已确认的错误,它阻止指定 newsequentialid()作为交互式表设计器中列的默认值。



请参阅: http://social.msdn.microsoft.com/Forums/en -US / sqltools / thread / cad8a4d7-714f-44a2-adb0-569655ac66e6



解决方法:创建您的表而不指定任何默认值,然后键入此T-SQL语句在正常查询窗口中运行它:

  ALTER TABLE dbo.YourTable 
ADD CONSTRAINT DF_SerialID DEFAULT newsequentialid()FOR serialID

这应该可以做到!


Basically I want to use uniqueidentifier in similar way as identity. I don't want to insert values into it, It should just insert values automatically, different value for each row. I'm not able to set autoincrement on columns of type uniqueidentifier(the property 'autoincrement' is set to false and is not editable).

解决方案

Or even better: use the newsequentialid() as the default for your UNIQUEIDENITIFER column. That'll give you a somewhat sequential series of GUIDs.

CREATE TABLE dbo.YourTable   
   (SerialID UNIQUEIDENTIFIER 
        CONSTRAINT DF_SerialID DEFAULT newsequentialid(),
     .... (other columns)......
   )

Trouble is: newsequentialid is only available as a column default - you cannot call it as a function or anything. But that seems to fit your requirements.

UPDATE: there appears to be an acknowledged bug in SQL Server Management Studio that prevents specifying newsequentialid() as the default for a column in the interactive table designer.

See: http://social.msdn.microsoft.com/Forums/en-US/sqltools/thread/cad8a4d7-714f-44a2-adb0-569655ac66e6

Workaround: create your table without specifying any default, and then type in this T-SQL statement in a normal query window and run it:

ALTER TABLE dbo.YourTable
    ADD CONSTRAINT DF_SerialID DEFAULT newsequentialid() FOR SerialID

That should do the trick!

这篇关于自动增量uniqueidentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆