简体   繁体   中英

How do I auto increment the primary key in a Microsoft SQL Server Management Studio database table, the datatype is declared as uniqueidentifier

如何在Microsoft SQL Server Management Studio数据库表中自动增加主键,数据类型声明为uniqueidentifier,我无法将其更改为int以使用身份规范,也无法更改表,因为它已连接到其他表外键,我已经浏览了论坛,但看不到如何。

You cannot 'increment' a UniqueIdentifier. Instead you can generate new value by

DECLARE @GUID uniqueidentifier
SET @GUID = NEWID()

Use the value in @GUID variable everytime insert is done.

INSERT INTO tableName(primaryKeyColumn,otherColumn1,otherColumn2)
VALUES(@GUID,val1,val2)

See this article IDENTITY vs UNIQUEIDENTIFIER

This was the best answer for this topic...I hope it will help

Autoincrement uniqueidentifier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM