How do I add the identity property to an existing column in SQL Server

Tristan Havelick picture Tristan Havelick · Nov 13, 2008 · Viewed 73.5k times · Source

In SQL Server (in my case, 2005) how can I add the identity property to an existing table column using T-SQL?

Something like:

alter table tblFoo 
    alter column bar identity(1,1)

Answer

JohnFx picture JohnFx · Nov 13, 2008

I don't beleive you can do that. Your best bet is to create a new identity column and copy the data over using an identity insert command (if you indeed want to keep the old values).

Here is a decent article describing the process in detail: http://www.mssqltips.com/tip.asp?tip=1397