Question : Change Table Column to identity

In a bit of a panic with a live system. SQL Server 2008 database - have data in table where the first column is currently a varchar(10) columnn.

Urgently need to load that same data (keeping the same values) into a new table with exactly the same structure except that the first column will be an INT IDENTITY column.

How can I go about doing this, please ?

Answer : Change Table Column to identity

you cannot alter a table from/to identity...

so, if the column contains already the data, you have to create the new table, and to fill it, you need to use the SET IDENTITY_INSERT "trick":
http://msdn.microsoft.com/en-us/library/ms188059.aspx
http://msdn.microsoft.com/en-us/library/ms176057.aspx
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
-- create table NEW_TABLE ...etc

SET IDENTITY_INSERT new_table ON

INSERT INTO new_table ( id, col1, col2 ... etc )
SELECT old_id, col1, col2, ... etc 
  FROM old_table

SET IDENTITY_INSERT new_table OFF

DBCC CHECKIDENT ( new_table , reseed )
Random Solutions  
 
programming4us programming4us