11

I have two databases, one is called Natalie_playground and one is called LiveDB. Since I want to practice insert, update things, I want to copy some of the tables from the LiveDB to Natalie_playground.

The tables I want to copy are called: Customers, Computers, Cellphones, Prices

What I tried to do is that (using SSMS) right click on a table but there is no Copy in there!

5
  • In both databases exists the same tables (with the same structure)? only you want pass only a data? Commented Jul 11, 2013 at 0:13
  • 2
    possible duplicate of SQL Server 2008: copying the contents of all tables from one database into another database and How to copy one table from a database to another database/table in SQL Server, both of which are listed in the Related list to the right of your question. ===>>> Please do at least a basic search here before posting a new question to see if it has already been asked (and answered) before. Thanks. Commented Jul 11, 2013 at 0:24
  • @natalia, Ken have reason, for this question, normally the answers already exists. Thanks. Commented Jul 11, 2013 at 0:42
  • I already did that research, as a beginner I did not know what is that .. in the syntax of: How to copy one table from a database to another database/table in SQL Server. So this is why I asked and got a syntax at least I can understand. Commented Jul 11, 2013 at 15:16
  • take a look at David's answer: stackoverflow.com/questions/187770/… Commented Apr 6, 2015 at 18:38

7 Answers 7

12

Assuming that you have two databases, for example A and B:

  • If target table not exists, the following script will create (I do not recommend this way):

    SELECT table_A.FIELD_1, table_A.FIELD_2,......, table_A.FIELD_N INTO COPY_TABLE_HERE FROM A.dbo.table_from_A table_A 
  • If target table exists, then:

     INSERT INTO TABLE_TARGET SELECT table_A.FIELD_1, table_A.FIELD_2,......, table_A.FIELD_N FROM A.dbo.table_from_A table_A 

Note: if you want learn and practice this, you can use previous scripts, but if you want copy the complete structure and data from database to another, you should use, "Backup and restore Database" or, "Generate Script Database with data" and run this into another database.

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

1 Comment

Can you do this without selecting the field names explicitly?
10

Right click on your database -> under Tasks choose Generate scripts, follow the wizard, choose your tables and check the check box that says 'script table data' (or similar) generate it to an SQL script and execute it on your other DB.

Comments

4

You can also try SQL Server Import/Export wizard. If target tables do not exist already they will be created when you run the wizard.

Check out MSDN for more details http://msdn.microsoft.com/en-us/library/ms141209.aspx

1 Comment

Probably the simplest and least time consuming. +1
1

I found an easy way from other blog. Hope this might be helpful.

Select * into DestinationDB.dbo.tableName from SourceDB.dbo.SourceTable 

http://www.codeproject.com/Tips/664327/Copy-Table-Schema-and-Data-From-One-Database-to-An

Comments

1

Try this:

If target table is exists :

SELECT SourceTableAlias.* INTO TargetDB.dbo.TargetTable FROM SourceDB.dbo.SourceTable SourceTableAlias 

And if target table is not exists :

 INSERT INTO TargetDB.dbo.TargetTable SELECT SourceTableAlias.* FROM SourceDB.dbo.SourceTable SourceTableAlias 

Good Luck!

Comments

0

try this

USE TargetDatabase GO INSERT INTO dbo.TargetTable(field1, field2, field3) SELECT field1, field2, field3 FROM SourceDatabase.dbo.SourceTable WHERE (some condition) 

Comments

-1

If you want to copy only the schema of tables, you can add a false condition to the end of mentioned queries.

ex.

SELECT table_A.FIELD_1, ..., table_A.FIELD_N INTO LiveDB.custom_table FROM Natalie_playground.dbo.custom_table table_A WHERE 0 > 1 

1 Comment

I know I am late to the party but this is going the WRONG way around. It would make changes to the LiveDB.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.