We have a 5GB table (nearly 500 million rows) and we want to remove the identity property on one of the column, but when we try to do this through SSMS - it times out.
Can this be done through T-SQL?
We have a 5GB table (nearly 500 million rows) and we want to remove the identity property on one of the column, but when we try to do this through SSMS - it times out.
Can this be done through T-SQL?
I just had this same problem. 4 statements in SSMS instead of using the GUI and it was very fast.
Make a new column
alter table users add newusernum int;
Copy values over
update users set newusernum=usernum;
Drop the old column
alter table users drop column usernum;
Rename the new column to the old column name
EXEC sp_RENAME 'users.newusernum' , 'usernum', 'COLUMN';
You cannot remove an
IDENTITY
specification once set.To remove the entire column:
Information about ALTER TABLE here
If you need to keep the data, but remove the
IDENTITY
column, you will need to:IDENTITY
column to the new columnIDENTITY
column.Bellow code working as fine, when we don't know identity column name.
and want to copy data into new temp table like Invoice_DELETED. and next time we using: insert into Invoice_DELETED select * from Invoice where ...
special thanks to 'Andriy M'
for more explanation see: https://dba.stackexchange.com/a/138345/101038
Following script removes Identity field for a column named 'Id'
Hope it helps.
This gets messy with foreign and primary key constraints, so here's some scripts to help you on your way:
First, create a duplicate column with a temporary name:
Next, get the name of your primary key constraint:
Now try drop the primary key constraint for your column:
If you have foreign keys, it will fail, so if so drop the foreign key constraints. KEEP TRACK OF WHICH TABLES YOU RUN THIS FOR SO YOU CAN ADD THE CONSTRAINTS BACK IN LATER!!!
Once all of your foreign key constraints have been removed, you'll be able to remove the PK constraint, drop that column, rename your temp column, and add the PK constraint to that column:
Finally, add the FK constraints back in:
El Fin!
Just for someone who have the same problem I did. If you just want to make some insert just once you can do something like this.
Lets suppose you have a table with two columns
and want to insert a row with the ID = 4. So you Reseed it to 3 so the next one is 4
Make the Insert
And get your seed back to the highest ID, lets suppose is 15
Done!