Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy data between two server instances

Tags:

I want something like :

insert into server2.database1.table1 select * from server1.database1.table1

both tables are exactly the same.

how can I Copy data between two server instances?

like image 964
Mahdi Tahsildari Avatar asked Dec 22 '12 12:12

Mahdi Tahsildari


People also ask

How do I transfer data from one instance to another?

Step 1: Right click on any database and select Tasks option and in Tasks select Copy Database. If you get the following error that means your SQL Server Agent is not running. For copying the database it is required that SQL Server instance must run. To remove the above error run your SQL Server instance.

How do I copy a SQL Server database to another server?

Right-click on the database and select Tasks and then Copy Database. Once you click on Copy Database then the following screen will appear. Click on "Next". Enter the Source server name (for example, Server A) and enter the Server Authentication mode and click on "Next".


2 Answers

SQL - Linked Server

If both servers are SQL Server, you can set up Linked servers - I would suggest using an SQL account for security there.

Then you can simply perform

insert into server2.database1.dbo.table1  select * from server1.database1.dbo.table1 where col1 = 'X' 

If you run the query in SQL Management studio connected to server1, and current database set to database1, you won't need the prefix

server1.database1.dbo. 

Also, the linked server would be configured on server1, to connect to server2 (rather than the other way around).

If you have the correct OLE DB drivers, this method can also work between different types of RDBMS (ie. non-SQL Server ones).

Open Query

Note: Beware not to rely on linked servers too much especially for filtering, and for joins across servers, as they require data to be read in full to the originating RDBMS before any conditions can be applied. Many complications can arise from Linked Servers, so read up before you embark, as even version differences might cause headaches.

I recommend you use the OPENQUERY command for SQL Servers to get around such limitations. Here's an example, but you should find help specific to your needs through further research:

insert into server2.database1.dbo.table1  select * from OPENQUERY(server1, 'select * from database1.dbo.table1 where col1 = ''X'''); 

The above code is more efficient, filtering the data on the source server (and using available indexes), before pumping the data through, saving bandwidth/time/resources of both the source and destination servers.

(Also note the double quote '', is an escape sequence to produce a single quote.)

SQL - Temporarily on the same server

Would enable (note the underscore):

insert into server2_database1.dbo.table1  select * from database1.dbo.table1 

Still within the SQL query domain. If you can temporarily move the database on server2 to server1, then you won't need the linked server. A rename of the database would appear to be required while co-locating on server1. Achieving such co-location could use various methods, I suggest shrinking database files before proceeding with either:

  1. Backup/Restore - Backup on server2, Restore on server1 (with different name) - perform insert as described above, but without the server1 or server2 prefixes. Then reverse - backup on server1, restore on server2/
  2. Detach/Attach - Rename database, Detach on server2, (compress), copy files to server 1, (decompress), attach on server1, perform insert. Then reverse...

In either case, SQL Server version could be a barrier. If server1 is of a lower SQL version, then both backup and detach/attach methods will likely fail. This can be worked around by moving the server1 database to server2, which may or may not be more suitible.

Other Methods

May be suitable, non-SQL/TSQL method failing favorable environmental factors for previously mentioned methods. And if you have the correct access (OLE DB Drivers, etc..), this method can also work between different types of RDBMS (ie. non-SQL Server ones), and data-sources (such as XML, flatfiles, Excel Spreadsheets...)

  • SSIS Explicitly with Business Development Management Studio - direct datapump or using delimited file intermeditary.
  • SSIS Implicitly through SQL Management Studio, by right clicking the database1 on server1 > Tasks > Export, then completing the wizard. May work direct to server2, or using a flat-file intermeditary.
  • .Net Programming with SqlBulkInsert (I believe the SSIS datapump uses such an object), I can go into more detail about this, if it interests you.

Eg. of SQLBulkInsert (psedo-C# code)

SqlConnection c = new SqlConnection("connectionStringForServer1Database1Here"); SqlConnection c2 = new SqlConnection("connectionStringForServer2Database1Here"); c.Open(); SqlCommand cm = new SqlCommand(c); cm.CommandText = "select * from table1;"; using (SqlDataReader reader = cm.ExecuteReader()) {     using (SqlBulkInsert bc = new SqlBulkInsert(c))     {          c2.Open();          bc.DestinationTable = "table1";          bc.WriteToServer(reader);     } } 

Pretty cool huh? If speed/efficiency is a concern - SqlBulkInsert based approaches (Such as SSIS) are the best.

Update - Modifying the destination table

If you need to update the destination table, I recommend that you:

  1. Write to a staging table on the destination database (a temporary table, or proper table which you truncate before and after process), the latter is preferable. The former may be your only choice if you don't have CREATE TABLE rights. You can perform the transfer using any one of the above options.
  2. Run a MERGE INTO command as per your requirements from the staging table to the destination table. This can Insert, Update and Delete as required very efficiently.

Such a whole process could be enhanced with a sliding window (changes since last checked), only taking recently changed rows in the source an applying to the destination, this complicates the process, so you should at least accomplish the simpler one first. After completing a sliding window version, you could run the full-update one periodically to ensure there are no errors in the sliding window.

like image 152
Kind Contributor Avatar answered Sep 19 '22 18:09

Kind Contributor


To copy data between two different servers you have several options:

  • Use linked servers.
  • Use the data import export wizard.
  • Use a third party tool such as Red Gate SQL Data Compare.
like image 40
Mark Byers Avatar answered Sep 20 '22 18:09

Mark Byers