Link to home
Start Free TrialLog in
Avatar of santa-clara
santa-clara

asked on

How do I dynamically clone a table, update the identity column (add 200,000 to it), finally reseed the identity column

Unfortunately I have discovered that a previously 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 = ident-id + 200000"
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 column & then reseed it?
Avatar of Faiga Diegel
Faiga Diegel
Flag of United States of America image

You have to manually script it.

--create constraint
ALTER TABLE dbo.CloneTable
ADD CONSTRAINT [PK_ColumnID]
PRIMARY KEY CLUSTERED (CoulunID)
GO

--drop constraint
ALTER TABLE dbo. CloneTable
DROP CONSTRAINT PK_ColumnID
GO

--reseed indentity
DBCC CHECKIDENT('[dbo].[IDV_CloneTable]', RESEED, 200000)
Avatar of santa-clara
santa-clara

ASKER

so you are saying that it is not possible to dynamically clone a table?

Plus I couldn't figure out how to add the identity attribute without dropping the column first
ASKER CERTIFIED SOLUTION
Avatar of Ernariash
Ernariash
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well I now understand that I have to insert into the cloned table recalculating the new identity value as I do so.
This just leaves the rather awkward problem of cloning the table in the first place.
Dynamically
This would seem to be a case for SSMO since SSMS is able to generate the SQL required.
I would appreciate a being pointed in the right direction on this
>>since SSMS is able to generate the SQL required.<<
Than why don't you use the same SQL script SSMS uses?
Please don't give me that "why do you want to do that?" whine
I want to dynamically clone a table.
Period
Fair enough.

One way is to simply do an insert into... then fix up indexes...

e.g.

if object_id('ee_clone_receiver','U') is not null drop table ee_clone_receiver

select  identity(int,200000,1) as id, date into ee_clone_receiver from ee_clone_doner

kind of depends on table structures a bit - there are foreign keys, addiotnal indexs etc - it is possible to script it all up so that you can simply do a "maketable" type stored procedure - there are a few versions of that floating around - want me to dig one up ?
exec sp_MakeTable 'OrignalTbl'   'CloneTbl' is certainly what I am looking for

But it does have to be able to  reproduce the original's

Constraints, Indexes, Calculated fields and triggers

just like the script generated by SSMS
part of the real challenge is error handling - there can be various constraints that do not exist. "smart" packages will highlight / flag etc and ask you what you want to do (or auto create dependancies) and that is why some of thos packages can get away with charging reasonable dollars...

Is it within the same database and frequently run, is it data as well as structure ?

Not sure that the simple editions will go to the extent you are looking for...  Then it might be best to compare a few commerical packages.

At the risk of being shouted at, curious what you are upto... can you spare a moment to describe what you are doing ? Sounds interesting...
well ... to considerably over-simplify (and yes I am fully conversant with the concepts of Normalization)
the original application simply cloned a database so that all the user application had to decide was "Which Database?"
This is starting to get out of hand because the Database contains some truly global tables which end up being "cloned" and the contents replicated.
This replication will be avoided if certain tables within the now single database can be cloned.
Once a table has been cloned ... it is quite likely to be enhanced ... however it is essential (from an O-O point of view) that they start off as true clones.
Since this appears to be pretty complicated from a raw T-SQL point of view ... I would say that my choices appear to be A) Powershell over SSMO or B) maintain the DDL in my own code.
to answer the Question ... the Data is required to be cloned as well but only for testing purposes.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well I guess I have been pointed in the right direction