简体   繁体   中英

copy tables with data to another database in SQL Server 2008

I have to copy the tables with data from one database to another using Query . I know how to copy tables with data within a database. But I was not sure about how to do the same for copying between two databases.

I have to copy huge number of tables, so I need any fast method using query...

Anybody please help out...Thanks in advance...

You can use the same way to copy the tables within one database, the SELECT INTO but use a fully qualified tables names database.schema.object_name instead like so:

USE TheOtherDB;

SELECT *
INTO NewTable
FROM TheFirstDB.Schemaname.OldTable

This will create a new table Newtable in the database TheOtherDB from the table OldTable whih belongs to the database TheFirstDB

  1. Right click on the database, select tasks and click on Generate Scripts.
  2. In the resultant pop-up, choose options as required (click advanced), to drop and create table, drop if exists, etc.
  3. Scroll down and choose "Schema and Data" or "Data Only" or "Types of data to script (2008 R2)"as required.
  4. Save to file and execute on the destination DB.

Advantages -

  • Can be executed against the destination DB, even if it is on another server / instance
  • Quickly script multiple tables, with data as needed

Warning - Might take quite a while to script, if the tables contain a large amount of data.

Rajan

INSERT INTO DB2.dbo.MyOtherTable (Col0, Col1)
SELECT Col0, Col1 FROM DB1.dbo.MyTable

Both table column's must have same data types..

Below SQL Query will copy SQL Server table schema & data from one database to another database. You can always table name (SampleTable) in your destination database.

SELECT * INTO DestinationDB.dbo.SampleTable FROM SourceDB.dbo.SampleTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM