In SQL 2005 and above (Enterprise and Developer Editions only), there is a very cool trick to solving this problem for large tables (e.g. a billion rows) which can save you hours of downtime.
I've used it to convert, in under 5 seconds, a column of a of a 2.5 billion row table from IDENTITY to a non-IDENTITY (in order to run a multi-hour query whose query plan worked better for non-IDENTITY columns), and then restored the IDENTITY setting, again in less than 5 seconds.
Here's the trick: you can use SQL Server's ALTER TABLE...SWITCH statement to change the schema of a table without changing the data, meaning you can replace a table with an IDENTITY with an identical table schema, but without an IDENTITY column. The same trick works to add IDENTITY to an existing column.
Normally, ALTER TABLE...SWITCH is used to efficiently replace a full partition in a partitioned table with a new, empty partition. But it can also be used in non-partitioned tables too.
Here's a code sample of how it works.
CREATE TABLE Test
(
id int identity(1,1),
somecolumn varchar(10)
);
INSERT INTO Test VALUES ('Hello');
INSERT INTO Test VALUES ('World');
-- copy the table. use same schema, but no identity
CREATE TABLE Test2
(
id int NOT NULL,
somecolumn varchar(10)
);
ALTER TABLE Test SWITCH TO Test2;
-- drop the original (now empty) table
DROP TABLE Test;
-- rename new table to old table's name
EXEC sp_rename 'Test2','Test';
-- see same records
SELECT * FROM Test;
This is obviosuly more involved than the solutions above, but if your table is large this can be a real life-saver. There are some caveats:
- you'll need to drop foriegn keys before you do the switch and restore them after.
- same for WITH SCHEMABINDING functions, views, etc.
- new table's indexes need to match exactly (same columns, same order, etc.)
- old and new tables need to be on the same filegroup.
- I mentioned this above, but this only works on the Enterprise or Developer editions of SQL Server 2005 (or 2008 or later).
There's a good article on TechNet detailing the requirements above.