Match table data on LinkedServer servers

0

I have 3 LinkedServer servers (the 3 having the same tables with the same fields). Home Server 1: First_Server Home Table: Testing

Server2:Second_ServerHomeTable:Testing

Server3:Third_ServerHomeTable:Testing

Selecttoreturnthefieldsfromthe"Test" table in "First_Server":

SELECT id, username, password FROM OPENQUERY(Primeiro_Servidor, 'SELECT id, username, password FROM Teste')

I would like to take the table "Test" from server 1 for example, and replicate all its data in the table "Test" of servers 2 and 3 (checking if the id exists, if there is an update, if not exist, insert). Could someone tell me how I can do this?

    
asked by anonymous 16.07.2018 / 16:06

2 answers

2

A typical replication solution involves using two commands in the same transaction, one to update existing rows in the remote table and one to add non-existent rows to the remote table:

-- código #1
BEGIN TRANSACTION;

-- atualiza informações existentes no servidor remoto (R)
UPDATE R
  set username= L.username,
      password= L.password 
  from Segundo_Servidor.banco.esquema.Teste as R
       inner join Teste as L on R.id = L.id
  where L.username <> R.username
        or L.password <> R.password;

-- acrescenta informações inexistentes no servidor remoto (R)
INSERT into Segundo_Servidor.banco.esquema.Teste (id, username, password)
  SELECT L.id, L.username, L.password
    from Teste as L
    where not exists (SELECT * 
                        from Segundo_Servidor.banco.esquema.Teste as R
                        where R.id = L.id);

COMMIT;

The above code runs on the local server (L), that is, the first server. That is, it pushes the data to the remote (R) server.

Another solution is possible, with replication code running on each remote server, pulling the data:

-- código #2
BEGIN TRANSACTION;

-- atualiza informações existentes no servidor local
UPDATE L
  set username= PS.username,
      password= PS.password 
  from Primeiro_Servidor.banco.esquema.Teste as PS
       inner join Teste as L on PS.id = L.id
  where L.username <> PS.username
        or L.password <> PS.password;

-- acrescenta informações inexistentes no servidor local
INSERT into Teste (id, username, password)
  SELECT PS.id, PS.username, PS.password
    from Primeiro_Servidor.banco.esquema.Teste as PS
    where not exists (SELECT * 
                        from Teste s L
                        where PS.id = L.id);

COMMIT;
The advantage of code # 2 is that it can be transformed into stored procedure on the main server and executed remotely with EXECUTE AT from the main server. That is, a single version of the code, maintained on the primary server, but run on each remote server from the primary server:

-- código #3
EXECUTE (...) AT Segundo_Servidor;
EXECUTE (...) AT Terceiro_Servidor;

However, either by pushing or pulling the data, this type of replication generates excessive network traffic because to know whether or not the line exists on the practice the contents of the table (remote or local, depending on whether pushing or pulling ) travels across the network.

There are ways to optimize the above codes. But the suggestion is to evaluate the implementation of some kind of automatic replication provided by SQL Server.

PS: I have not tested the solutions proposed above; I hope they do not contain an error.

    
17.07.2018 / 12:54
1

You can do a direct insert from a select of course but why do you do this if you have a free tool to do MS SQL Server data comparison?

Any version of Visual Studio has a tool for data comparison and schema that can connect to two bases and compare the differences.

Selecttwobasesthatcanbeontwodifferentservers...

Youchoosethetablesthatwillbecompared,notethatinordertocompareregistry-to-registry,thetablemusthaveaPKoratleastasinglekey(wellanyway,knowwhichregistertocomparewith).>

Youcanevenapplythedifferencesbymatchingthedataorgeneratingascriptthatwilldothis.

Inaddition,therearelotsof documentation on how to use this feature. Even if you do not have Visual Studio (which any developer working with .Net will have) installed, the download of this version I showed you is free.

    
17.07.2018 / 13:09