SQL Server - Copy Table in Same Database

Status
Not open for further replies.

DeltaHuey

Ars Tribunus Militum
1,693
Hello,

I need to copy a mass amount of tables in SQL Server to the same database (obviously renaming the tables). I need to include constraints, keys, indexes, etc, as well as data.

So for example:

ACGPTL_Affiliates => AFPPTL_Affiliates

(we need to copy an old dot net nuke site, and our host is limited to one database)

First thought was just 'SELECT * INTO (new table) FROM (old table)', but this would just be data only, correct?

Second thought was using the 'Script Table As -> Create To' task. The output of this looks like what I want. The problem is I couldn't recreate the data properly, right? Or am I wrong? My feeling was if I just followed this with a 'SELECT * INTO (new table) FROM (old table)' I would be screwed due to the PK and constraints being missed.

Any thoughts? Is there a supported way to do this? I have access to intermediate databases, but ultimately both tables need to reside in a single database.

Thanks~
 
D

Deleted member 43669

Guest
DeltaHuey said:
Second thought was using the 'Script Table As -> Create To' task. The output of this looks like what I want. The problem is I couldn't recreate the data properly, right? Or am I wrong? My feeling was if I just followed this with a 'SELECT * INTO (new table) FROM (old table)' I would be screwed due to the PK and constraints being missed.
/quote]

If you do it in the proper order, I don't think you'd be screwed. However, you need to think about foreign key constraints... they should refer to the new copies, right?
 

Rivan

Ars Scholae Palatinae
1,399
Do a combination of the two.

Use the script part because it will generate indexes, primary keys, and foreign keys (which you may have to update as well, remember). You will not get that with "select into", you will only get the data and column names.

Instead of select into, you just need INSERT [NewTable] SELECT * FROM [OldTable] to move the data. As long as the column structure is identical, this will work.
 

TheDan666

Ars Tribunus Militum
2,359
Don't know if this will work for you or not. But you can try the Database Copy Wizard:

http://msdn.microsoft.com/en-us/library/ms188664.aspx

It may be able to just create the script it will run to execute the task. I don't have SQL Server do I don't know. If it does create the script then you can just replace the table names to avoid the name conflicts.

--The Dan
 

technophile

Ars Legatus Legionis
23,768
You can use the "SET IDENTITY_INSERT [TableName] ON" statement to enable inserting the original PK values even if the field is set to autoincrement. Just remember to run the corresponding OFF statements after, and you may need to reset the autoincrement seed so it doesn't step on the imported values after.

See here for more info on both parts of that.
 

DeltaHuey

Ars Tribunus Militum
1,693
Thanks for the help. There are like 50 or more tables, so I'm not done yet ... lots of mundane work with scripts.

So here is what my plans are:

create all the new tables with proper primary key / identity constraints

set identity_insert to on

copy all of the data from the old tables to the new tables

set identity_isnert to off

create all of the other constraints (FK, unique, defaults, etc).
 
o7 Delta :)

I only have 2008 R2 on this machine - don't know which version you have.

You can right click the database, select Tasks, Generate Scripts.

Select tables only and next, and on the next dialogue look through the options - you should see one that says "Type of data to script" with choices of Data Only, Schema and Data and Schema only - you'll want Schema and Data.


This should generate the bulk of your work - you'll have to do some tedious manual editting for table names, but hopefully its a good start
 
Status
Not open for further replies.