What's the most efficient way to copy a large amount of rows from a table in one database to another table in a different database that has the exact same structure?
A simple way is to open SSMS and Right click on database and go to Tasks > Import Data and follow the wizard to set source and destination. This way if data exists on different systems and even if you wish to change structure you can do. Also append, or cleanout data at same time from destination.
Steps that need to be followed are:Launch SQL Server Management Studio. Select and right-click on the Source Database, go to Tasks > Export Data. Import/Export Wizard will be opened and click on Next to proceed. Enter the data source, server name and select the authentication method and the source database.
Using Backup and Restore to Copy a SQL Server Table to Another Server. You can copy the data to a temporary table in a new database in server-A, then backup this database and restore it in the destination server, and finally move the data from the restored database into the real destination table.
If the databasesare on the same server, then it's trivial - you'd do it as if you were copying between tables in the same database, i.e.:
INSERT INTO targetdatabase..targettable (col1, col2, col3, col4) SELECT col1, col2, col3, col4 FROM sourcedatabase..sourcetable
If the databases are on different servers, you'll need to look at using one of OPENQUERY, OPENROWSET or linked servers to do the query, but fundamentally even with all these, you'd still be writing a variation on the command above.
Alternatively, it's a case of BCP out and BCP in or using SQL Server Management Studio's data transfer wizard.
If your log IO subsystem allows it, then:
INSERT INTO target(field1, field2, field3) SELECT field1, field2, field3 FROM source;
But having the entire transfer occur in one single statement means one single transaction, which means that x2.5 data size log has to be generated and kept during the entire statement. Ie. if you transfer 50Gb, your target db log will grow to 250Gb (even if the recovery mode is set to simple!).
If you are concerned about log, then you have to transfer in batches. You can still do the INSERT ... SELECT trick, but your SELECT has to use some key range and batch acceptable number of rows.
Ultimately, you can always do a bcp out
followed by a bcp in
, it will work pretty fast and is probably the fastest way to get a reliable transfer going.
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