2

How do I auto increment the primary key in a Microsoft SQL Server Management Studio database table, the datatype is declared as uniqueidentifier and I could not change it to int to use Identity Specification also I could not alter table because it is connected to other table as a foreign key , I've had a look through the forum but can't see how.

user3390299
  • 83
  • 1
  • 2
  • 6
  • possible duplicate of [Autoincrement uniqueidentifier](http://stackoverflow.com/questions/2837559/autoincrement-uniqueidentifier) – mmarie Mar 19 '14 at 12:34

2 Answers2

2

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

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
-1

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

Autoincrement uniqueidentifier

Community
  • 1
  • 1
ASNAOUI Ayoub
  • 462
  • 2
  • 9