Updating identity column in SQL Server and setting the seed starting value

I have a table filled with data and one of the columns - TrackingNumber - is an integer value. I have gotten a request to change it to auto-increment and to have it start the identity seed at 1000000. I know that I cannot alter a column to be an identity column in an existing table with data, so I have two options: either create an entirely new table and then move data from the old table into that new table or add an new identity column and update it with data from the old column.

The problem is that I need to retain all the existing values in column TrackingNumber. I have tried the following:
    1) ALTER TABLE [dbo].[Table1]
    ADD [TrackingNumber2] [bigint] IDENTITY (1000000, 1) NOT NULL

    2) UPDATE [dbo].[Table1]
    SET [TrackingNumber2]=[TrackingNumber]

    3) ALTER TABLE [dbo].[Table1]
    DROP COLUMN [TrackingNumber] 
    GO

    4) EXEC sp_rename 'Table1.TrackingNumber2', 'TrackingNumber','COLUMN'

Open in new window

I got an error on step 2 - Updating new column with the value from the old column: "Cannot update identity column 'TrackingNumber2'"

Can anyone recommend a workaround?
LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
You need to turn the IDENTITY_INSERT option to on before the update, like this:
SET IDENTITY_INSERT Table1 ON
UPDATE [dbo].[Table1]
    SET [TrackingNumber2]=[TrackingNumber]
SET IDENTITY_INSERT Table1 OFF

Open in new window

0
Vikas GargBusiness Intelligence DeveloperCommented:
Hello,

You can use CTE and row_number function for the same

;With CTE as
(select *,row_number()over(order by TrackingNumber) rn + 1000000 from Table)

update cte set TrackingNumber2 = RN
0
Jagdish DevakuSr DB ArchitectCommented:
Hi,

Please try the following:

1. Create a new table "temp" with the same structure of "Table1" but set the "TrackingNumber" to identity on creation

2. Insert the data from "table1" to "temp"
Ex:      
      GO
      SET IDENTITY_INSERT [dbo].temp ON
      GO
      INSERT INTO temp
      SELECT * FROM table1
      GO
      SET IDENTITY_INSERT [dbo].temp OFF
      GO

3. Drop the "table1"

4. Rename table "temp" to "table1"

Hope this works.

Thanks.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jagdish DevakuSr DB ArchitectCommented:
For the above steps make sure that you generate drop and create scripts of key (foreign keys, triggers).

1. first execute the Drop script for keys and triggers.

2. Follow the steps in the above comment.

3. Now execute the create script for keys and triggers.

Thanks.
0
YZlatAuthor Commented:
JagdishDevaku, I tried your suggestion and got en error:

An explicit value for the identity column in table 'dbo.temp' can only be specified when a column list is used and IDENTITY_INSERT is ON.

I tried listing all the columns and it worked!
0
Jagdish DevakuSr DB ArchitectCommented:
Great... Any other issues are you facing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.