Skip to main content
1 of 3
McNets
  • 23.9k
  • 11
  • 50
  • 89

There is a big problem that you have not considered:

  • As far as I know you cannot remove IDENTITY from a column.
  • Hence, you can only convert this column to int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0

Not good news, but you could try another approach:

  • Add a new column
  • Fill it with new values
  • Drop PRIMARY KEY constraint.
  • Drop ID column
  • Rename new column as Id
  • Add a PRIMARY KEY constraint on Id column
CREATE TABLE MyTable
(
    Id int IDENTITY(1,1),
    F1 int,
    CONSTRAINT [PK_MyTable] PRIMARY KEY (Id)
);

INSERT INTO MyTable VALUES (10),(20),(30);

--= Add a new column
ALTER TABLE MyTable ADD Id2 uniqueidentifier DEFAULT NewID() NOT NULL;

--= Fill new field with values
UPDATE MyTable 
SET    Id2 = NewID();

--= Drop PRIMARY KEY
ALTER TABLE MyTable DROP CONSTRAINT [PK_MyTable];

--= Drop Id column
ALTER TABLE MyTable DROP COLUMN Id;

--= Rename Id2 column as Id
EXEC sp_RENAME 'MyTable.Id2', 'Id', 'COLUMN';

--= Add PRIMARY KEY
ALTER TABLE MyTable ADD CONSTRAINT [PK_MyTable] PRIMARY KEY (Id);
    
SELECT * FROM myTable;
GO
F1 | Id                                  
-: | :-----------------------------------
30 | e430b87c-6fbf-43c3-a88c-00abe925c2ea
20 | 67e6d0ac-3bb8-4c29-b19b-501ebdc2acee
10 | 287117fb-0188-480f-be54-ef42cacc123a

Caution: Changing any part of an object name could break scripts and stored procedures.

dbfiddle here

Editing...

McNets
  • 23.9k
  • 11
  • 50
  • 89