Thursday, June 03, 2010

Removing Identity Column from a large table

To disable an identity column we basically rely on management studio, which intern create temp table transfer the data back and forth for us. But if the table is bit huge with millions of rows, you will get a time out error. Normally most of the solution given by people would be to create a temp table with no identity column transfer the rows into it and then rename the table.



The catch in this is , there is a very bright chance of you missing some relationship as you have to create them manually. So the innovative way invented by meeeeeeeeee is ;) (tanta da tanta da doiiiiii hehe back ground music) is the below


1. Create a teamporary table as shown below


select * into temp from main




2. select count(*) from temp


select count(*) from main



check if the rows are equal just in case




3. Truncate table main

Remove the identity property from the GUI.



4, ALTER TABLE main
NOCHECK CONSTRAINT ALL


INSERT into main 
select * from temp


ALTER TABLE main
CHECK CONSTRAINT ALL






5. select count(*) from main


select count(*) from temp



check if the rows are equal just in case




6. Drop table temp  -- Dont put the main table name then ur inviting a disaster


Thats it for the day folks. Hope to see you all soon with some thing uninteresting ;).Until then its me signing off



With Smiles
Santhosh

0 Secrets: