Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag for Brazil asked on

Is it possible to make a SQLServer table's PK column to be indentity after the table is populated?

Hi Experts!

Is it possible to transform an existing PK column to be an indentity column too, after the table is populated?

How could I do that?

Thanks in advance
Microsoft SQL Server

Avatar of undefined
Last Comment
Eduardo Fuerte

8/22/2022 - Mon
Kent Olsen

That would depend on the version of SQL Server that you're running.

On 2008 and older, no.
Eduardo Fuerte

ASKER
I'm using 2014, could you point the command (or a reference to it) ?
ASKER CERTIFIED SOLUTION
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

As far as I know, there is no magic one-line command.  But you can still do it.  If there are no foreign keys that reference that column, the easiest is to

ALTER TABLE {mytable} ADD {newcolumn} INT IDENTITY(1,1) NOT NULL;

Copy the values in the PK to the new column.
Drop the old PK column.
Rename the new column to the same name as the dropped column.

If there are foreign keys against the table, it gets a lot more involved.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Eduardo Fuerte

ASKER
Hi

Going the easyest way

img001
It's not possible to update the identity column with the old values...
Kent Olsen

Set IDENTITY_INSERT ON

and you should be able to update the row to have the values in the original row.

Remember to set IDENTITY_INSERT OFF again when you're done!  :)
Eduardo Fuerte

ASKER
Even doing that

img002
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

I believe that you'll need to run both statements together.  Highlight them and run them in a single step.
SOLUTION
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Eduardo Fuerte

ASKER
Since I'm doing a migration data that must preserve the table codes, I have to go this way... just a little more time...
Kent Olsen

Also remember that this will affect any foreign keys that are in place.  You'll have to reestablish them.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Eduardo Fuerte

ASKER
Really a good solution the 2nd option you give.

Thanks for the guidance!