Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy tables from one database to another in SQL Server

I have a database called foo and a database called bar. I have a table in foo called tblFoobar that I want to move (data and all) to database bar from database foo. What is the SQL statement to do this?

like image 578
RyanKeeter Avatar asked Oct 09 '08 15:10

RyanKeeter


People also ask

How do I copy a table from one environment to another?

The easiest way is to right click "Linked Servers" in Management Studio; it's under Management -> Server Objects. This will both create the new table with the same structure as the original one and copy the data over.


1 Answers

SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.

If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes and such. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.

I use this all the time and it works fairly well.

like image 126
David Avatar answered Oct 07 '22 23:10

David