27

I've just created an empty database on my machine. I now wish to copy a table from our server database to this local database.

What sql commands do I need to run to do this? I wish to create the new table, copy data from the old table and insert it into the new table.

1

12 Answers 12

37

Create a linked server to the source server. The easiest way is to right click "Linked Servers" in Management Studio; it's under Management -> Server Objects.

Then you can copy the table using a 4-part name, server.database.schema.table:

select * into DbName.dbo.NewTable from LinkedServer.DbName.dbo.OldTable 

This will both create the new table with the same structure as the original one and copy the data over.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 only answer taking into account that those databases are on different servers!
Will not work if you're working with SQL Server 2000 and 2012+ connect.microsoft.com/SQLServer/feedback/details/731869/…
Solution for SQL Server 2000 and 2012+ here (answer with the highest vote, not the marked answer): stackoverflow.com/questions/187770/…
11

Assuming that they are in the same server, try this:

SELECT * INTO SecondDB.TableName FROM FirstDatabase.TableName 

This will create a new table and just copy the data from FirstDatabase.TableName to SecondDB.TableName and won't create foreign keys or indexes.

Comments

8

Another method that can be used to copy tables from the source database to the destination one is the SQL Server Export and Import wizard, which is available in SQL Server Management Studio.

You have the choice to export from the source database or import from the destination one in order to transfer the data.

This method is a quick way to copy tables from the source database to the destination one, if you arrange to copy tables having no concern with the tables’ relationships and orders.

When using this method, the tables’ indexes and keys will not be transferred. If you are interested in copying it, you need to generate scripts for these database objects.

If these are Foreign Keys, connecting these tables together, you need to export the data in the correct order, otherwise the export wizard will fail.

Feel free to read more about this method, as well as about some more methods (including generate scripts, SELECT INTO and third party tools) in this article: https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/

Comments

6

SELECT ... INTO :

select * into <destination table> from <source table> 

Comments

6

If migrating constantly between two databases, then insert into an already existing table structure is a possibility. If so, then:

Use the following syntax:

insert into DESTINATION_DB.dbo.destination_table select * from SOURCE_DB.dbo.source_table [where x ...] 

If migrating a LOT of tables (or tables with foreign keys constraints) I'd recommend:

  • Generating Scripts with the Advanced option / Types of data to script : Data only OR
  • Resort using a third party tool.

Hope it helps!

Comments

6
INSERT INTO ProductPurchaseOrderItems_bkp ( [OrderId], [ProductId], [Quantity], [Price] ) SELECT [OrderId], [ProductId], [Quantity], [Price] FROM ProductPurchaseOrderItems WHERE OrderId=415 

Comments

4

To copy the table from one database to another database, first you have to create the exact table structure for the new table as old one, than copy the table entries from one table to another.

Solution for MySQL database

create table [new database.]<new table name> like [old database.]<old table name>; insert [new database.]<new table name> select * from [old database.]<old table name>; 

Comments

2

Assuming that you want different names for the tables.

If you are using PHPmyadmin you can use their SQL option in the menu. Then you simply copy the SQL-code from the first table and paste it into the new table.

That worked out for me when I was moving from localhost to a webhost. Hope it works for you!

1 Comment

For very large tables whose SQL codes would be a pain to copy and paste, PHPmyadmin's Import and Export menus can easily be used as well.
1

If you need the entire table structure (not just the basic data layout), use Task>Script Table As>Create To>New Query Window and run in your new database. Then you can copy the data at your leisure.

Comments

1
insert into newdatabase.dbo.table select * from currenttable 

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
0

The quickest way is to use a tool, like RazorSQL or Toad for doing this.

Comments

0

This one line would be sufficient to create as well ass insert the data from the table of other database

create table as select * from .;

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.