0

Assume that we have two tables in two databases which are located in different database servers.

The situation is this:
Step 1: Select data from the first table.
Step 2: Iterating over the result of the first step
Step 2.1: For each row that is not present in the socond table
Step 2.2: If the result of step 2.1 is true, insert the row to the second table.

In case the tables are in same server the following SQL query works fine:

INSERT INTO server.table2(id, name, adresse) SELECT * FROM server.table1 WHERE table2.id NOT IN (SELECT id FROM server.table2) 

But I have a problem when the source table and destination table are distributed in two different database servers.

How can I realize the above described steps?

0

2 Answers 2

1

The best way to usually handle this is bulk copy in to a temporary table the data then you can do your normal query like you would.

I don't know your models so I leave it as a experiment to you on how to do that with a List, but if you could open two connections at once you can easily do it like this using SqlBulkCopy to transfer between the two servers.

using (var sourceConnection = new SqlConnection(sourceConnectionString)) using (var destinationConnection = new SqlConnection(destinationConnectionString)) { sourceConnection.Open(); destinationConnection.Open(); var createTableQuery = "create table #t (id uniqueidentifier, name nvarchar(100), adresse(nvarchar(100))"; using (var createTableCommand = new SqlCommand(createTableQuery, destinationConnection)) { createTableCommand.ExecuteNonQuery(); } using (var selectCommand = new SqlCommand("SELECT id, name, adresse FROM table1")) using (var selectReader = selectCommand.ExecuteReader()) using (var destBulkInsert = new SqlBulkCopy(destinationConnection)) { destBulkInsert.DestinationTableName = "#t"; destBulkInsert.WriteToServer(selectReader); } var mergeQuery = "INSERT INTO table2(id, name, adresse) SELECT * FROM #t WHERE #t.id NOT IN(SELECT id FROM table2)"; using (var mergeCommand = new SqlCommand(mergeQuery, destinationConnection)) { mergeCommand.ExecuteNonQuery(); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Scott, Thank you so much for answering my question. I tried like you suggested, bit i got an SqlExeption Error by executing the command ( createTableCommand.ExecuteNonQuery();) the error says : An unhandled exception of type 'System.Exception' occurred in SqlTestConnection.exe
Use the debugger, copy the exception details as a edit to your question
0
  1. On server1 create linked server and

    EXEC master.dbo.sp_addlinkedserver @server = N'server2', @srvproduct=N'SQL Server'

    EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'server2',@useself=N'False',@locallogin=NULL,@rmtuser=N'USER_NAME',@rmtpassword='########'

  2. Execute your query from server1 as

    use dbName1
    go

    INSERT INTO server2.dbName2.dbo.table2(id, name, adresse)
    SELECT * FROM dbo.table1 t1
    left join server2.dbName2.dbo.table2 t2 on t1.id=t2.id
    where t2.id is null

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.