自增唯一标识符
基本上我想以与身份类似的方式使用 uniqueidentifier.我不想在其中插入值,它应该只是自动插入值,每行不同的值.我无法在类型为 uniqueidentifier 的列上设置自动增量(属性自动增量"设置为 false 且不可编辑).
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).
推荐答案
或者更好:使用 newsequentialid()
作为 UNIQUEIDENITIFER 列的默认值.这将为您提供一系列有点顺序的 GUID.
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)......
)
问题是:newsequentialid 仅可用作列默认值 - 您不能将其作为函数或任何东西调用.但这似乎符合您的要求.
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.
更新:似乎在 SQL Server Management Studio 中存在一个公认的错误,该错误阻止将 newsequentialid()
指定为交互式表设计器中列的默认值.
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.
请参阅:http://social.msdn.microsoft.com/Forums/en-US/sqltools/thread/cad8a4d7-714f-44a2-adb0-569655ac66e6
解决方法:创建您的表而不指定任何默认值,然后在正常查询窗口中输入此 T-SQL 语句并运行它:
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
这应该可以解决问题!
相关文章