A typical replication solution involves using two commands in the same transaction, one to update existing lines in the remote table and one to add the missing lines in 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 is executed on the local server (L), that is, the first server. That is, it pushes the data for the remote server (R).
Another solution is also 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 the #2 code is that it can be turned into a procedure stored on the main server and run remotely with EXECUTE AT from the main server. That is, a single version of the code, maintained on the main server but run on each remote server from the main server:
-- código #3
EXECUTE (...) AT Segundo_Servidor;
EXECUTE (...) AT Terceiro_Servidor;
Meanwhile, be pushing or pulling the data, this type of replication generates excessive traffic on the network, because to know if the line exists or not on the other server, in practice the contents of the table (remote or local, depending on whether pushing or pulling) traffic through 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 contain no error.
These tables have relationships
– Jorge Costa
Have no relationships.
– Dark Stack
SQL server or Mysql
– Jorge Costa
Very important what kind of automatic replication, by running tasks
– Jorge Costa
They may not have a relationship, but probably the Id field is Identity with auto-increment... if you’ve done any Insert before in these other bases, you’ll need to turn it off and restart the index.
– Leandro Angelo
You can run the query using a
select
normal prefixing with the name on the link:select * from nome_link_server1.nome_banco.dbo.teste
and so it’s easy to do a Join and an update– Ricardo Pontual
If you want to match all tables, to avoid type validation
exists
, use the commandmerge
, that works with Linked Servers– Ricardo Pontual
Sqlserver... I put these 3 servers as an example, but it may occur that more servers enter. For this, I have a table that keeps the name of all Linked Servers servers
– Dark Stack