SQL Server add an identity key to existing table

by ebarcza 10/15/2009 11:54:00 AM

Say you got a table with 1000 records in then realize you did something real stupid(like I did) and not create a key or keyable compound fields. Welp you have to add an identity key but there's no easy way to back fill the entire 1000 records... Until now

step 1.) Script the table to create a new one with a slightly different name.

step 2.) add an identity key, seed it to 1

step 3.) insert all records from the old table into the new one. SQL server will fill the identity column just like you need them

step 4.) drop the old table without a key

step 5.) rename the new table

step 6.) go have a beer! you're done

 

Tags: ,

SQL SERVER

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  April 2018  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

View posts in large calendar

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2018

Sign in