Copy Table but change Identity seed

How do I clone a table so that the new table has a different identity seed and with the existing identity values changed programmatically

I have not been able to get "set identity on/off " to help here ... presumably because 2 tables are involved
(set new-id = 1160000 + old-id%1140000)

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Daniel WilsonCommented:
Not sure where the problem is on the 2-table setup.

Create Table NewTable(NewID int Not NULL Identity(1160000, 3),  field2, field3, etc)goSet identity_insert NewTable ongoinsert into NewTable(NewID, field2, field3, etc)(Select 1160000 + old-id%1140000, field2, field3, etc from OldTable)goSet Identity_Insert NewTable Offgo

Now ... your formula 1160000 + old-id%1140000 might create duplicates depending on the data in OldTable.  That could cause problems.

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
santa-claraAuthor Commented:
santa-claraAuthor Commented:
Unfortunately I have discovered that the accepted solution does not actually solve my problem.
The issue is the word "clone"
And a further unstated requirement is "Dynamic"
My attempts at a solution involved "select * into TableClone from TableOriginal"
followed by "update TableClone set  ident-id = 1160000+ident-id%1140000"
The big problem I have just discovered is that "select into" does not copy keys and defaults  ie it is not a "clone"
so I really seem to have 2 problems
1) how do I dynamically clone any given table?
2) how can I drop the Identity constraint, update the erstwhile identity col & then reseed it?
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.