I have a number of tables with data in Schema1
and I would like to copy these tables, with the data, to a new schema I have created, Schema2
.
Is there an elegant way of doing this? I am using SQL Server Management studio.
In SQL Management studio right click the database that has the source table, select Tasks -> Export data.
You will be able to set source and destination server and schema, select the tables you wish to copy and you can have the destination schema create the tables that will be exported.
Also, if copying to the same server and database but different schema be sure to:
Assuming the schema has structural changes, so you can't just do a backup and restore to a new database you can right-mouse click on the database and select tasks | generate scripts. On the second page there is an option (off by default) to script the data.
That creates a series of SQL insert statements along with the CREATE TABLE statements. You may also want to select Script Triggers and check a few of the other options that you may require if using those features.
You can also do an INSERT across schemas or use SSIS as described here:
How can I copy data records between two instances of an SQLServer database
One thing to be aware of is that I believe the export data facility doesn't propagate the indexes, constraints, triggers etc which is why I like to use the first method for moderate amounts of data or at least to create the new tables in the first place followed by one of the other methods.
Here is what I did:
This will not work for everyone because it requires the new schema to be available from the same connection in SQL Studio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With