I need to change the primary key of a table to an identity column, and there\'s already a number of rows in table.
I\'ve got a script to clean up the IDs to ensure
If the original poster was actually wanting to set an existing column to be a PRIMARY KEY
for the table and actually did not need the column to be an IDENTITY
column (two different things) then this can be done via t-SQL with:
ALTER TABLE [YourTableName]
ADD CONSTRAINT [ColumnToSetAsPrimaryKey] PRIMARY KEY ([ColumnToSetAsPrimaryKey])
Note the parenthesis around the column name after the PRIMARY KEY
option.
Although this post is old and I am making an assumption about the requestors need, I felt this additional information could be helpful to users encountering this thread as I believe the conversation could lead one to believe that an existing column can not be set to be a primary key without adding it as a new column first which would be incorrect.