36

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tristan Havelick
  • 67,400
  • 20
  • 54
  • 64

6 Answers6

31

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

JohnFx
  • 34,542
  • 18
  • 104
  • 162
  • You're correct, that isn't possible with the current versions of SQL Server. – Brent Ozar Nov 15 '08 at 12:19
  • @Brent, A side effect here is that the column order will change with this trick – Sam Saffron Dec 09 '10 at 00:16
  • column order is only an issue if you are doing "SELECT *" which I seem to recall is/is-going-to-be classified as a warning by SQL Server diagnostic tools (can't remember which one). – davek Dec 13 '10 at 19:56
  • @Sam, what is the bounty for? keeping the order of the columns? if so you need to create a temporary table, copy all data into it, then re-create the table with correct column ordering and copy the data back. Management Studio can do this for you, but that is only good if you do not a script. Visual Studio DB Edition can create a change script that takes care of re-ordering the columns... – Stefan Egli Dec 14 '10 at 18:14
  • @Sefen ... yes, the bounty is for a TSQL script that preserves order and simulates what management studio does. – Sam Saffron Dec 14 '10 at 21:41
  • @Brent - [It is possible](http://stackoverflow.com/questions/5181304/set-identity-on-the-column/6094907#6094907) – Martin Smith May 23 '11 at 09:06
  • @Martin - can you be a little less specific? ;-) How about an example? – Brent Ozar May 27 '11 at 17:42
  • @Brent - My comment had a hyperlink that doesn't show up very well! – Martin Smith May 27 '11 at 17:56
  • @Martin - wow, that's some crazy stunt driving! Not sure I'd be comfortable recommending that a client do that, but it's impressive. – Brent Ozar Jun 08 '11 at 16:42
12

The solution posted by Vikash doesn't work; it produces an "Incorrect syntax" error in SQL Management Studio (2005, as the OP specified). The fact that the "Compact Edition" of SQL Server supports this kind of operation is just a shortcut, because the real process is more like what Robert & JohnFX said--creating a duplicate table, populating the data, renaming the original & new tables appropriately.

If you want to keep the values that already exist in the field that needs to be an identity, you could do something like this:

CREATE TABLE tname2 (etc.)
INSERT INTO tname2 FROM tname1

DROP TABLE tname1
CREATE TABLE tname1 (with IDENTITY specified)

SET IDENTITY_INSERT tname1 ON
INSERT INTO tname1 FROM tname2
SET IDENTITY_INSERT tname1 OFF

DROP tname2

Of course, dropping and re-creating a table (tname1) that is used by live code is NOT recommended! :)

NateJ
  • 1,935
  • 1
  • 25
  • 34
  • This is also what is required for working with Azure SQL tables. – Larry Smithmier Jul 21 '10 at 19:03
  • first creating another table is the only way because if you tried to create another column and rename it, you can not update identity column with old data, Thanks – Mhmd Jul 26 '11 at 10:28
2

Is the table populated? If not drop and recreate the table.

If it is populated what values already exist in the column? If they are values you don't want to keep.

Create a new table as you desire it, load the records from your old table into your new talbe and let the database populate the identity column as normal. Rename your original table and rename the new one to the correct name :).

Finally if the column you wish to make identity currently contains primary key values and is being referenced already by other tables you will need to totally re think if you're sure this is what you want to do :)

Robert
  • 1,835
  • 4
  • 25
  • 30
2

There is no direct way of doing this except:

A) through SQL i.e.:

-- make sure you have the correct CREATE TABLE script ready with IDENTITY

SELECT * INTO abcTable_copy FROM abcTable

DROP TABLE abcTable

CREATE TABLE abcTable -- this time with the IDENTITY column

SET IDENTITY_INSERT abcTable ON

INSERT INTO abcTable (..specify all columns!) FROM (..specify all columns!) abcTable_copy

SET INDENTITY_INSERT abcTable OFF

DROP TABLE abcTable_copy 

-- I would suggest to verify the contents of both tables 
-- before dropping the copy table

B) Through MSSMS which will do exactly the same in the background but will less fat-fingering.

  • In the MSSMS Object Explorer right click the table you need to modify
  • Select "design" Select the column you'd like to add IDENTITY to
  • Change the identity setting from NO -> YES (possibly seed)
  • Ctr+S the table

enter image description here

This will drop and recreate the table with all original data in it. If you get a warning:

enter image description here

Go to MSSMS Tools -> Options -> Designers -> Table and database Designers and uncheck the option "Prevent saving changes that require table re-creation"

Things to be careful about:

  1. your DB has enough disk space before you do this
  2. the DB is not in use (especially the table you are changing)
  3. make sure to backup your DB before doing it
  4. if the table has a lot of data (over 1G) try it somewhere else first before using in real DB
Milan
  • 3,209
  • 1
  • 35
  • 46
2
  1. Create a New Table

    SELECT * INTO Table_New FROM Table_Current WHERE 1 = 0;
    
  2. Drop Column from New Table

    Alter table Table_New drop column id;
    
  3. Add column with identity

    Alter table Table_New add id int primary key identity; 
    
  4. Get All Data in New Table

    SET IDENTITY_INSERT Table_New ON;
    INSERT INTO Table_New (id, Name,CreatedDate,Modified) 
    SELECT id, Name,CreatedDate,Modified FROM Table_Current;
    SET IDENTITY_INSERT Table_New OFF;
    
  5. Drop old Table

    drop table Table_Current;
    
  6. Rename New Table as old One

    EXEC sp_rename 'Table_New', 'Table_Current';
    
barbsan
  • 3,418
  • 11
  • 21
  • 28
Anuj Kumar
  • 21
  • 1
-5
alter table tablename 
alter column columnname 
add Identity(100,1)
a_m0d
  • 12,034
  • 15
  • 57
  • 79