· 7 years ago · Sep 02, 2018, 10:40 AM
1replication between two tables with different names and which have different column names. Is it possible to create such replication
2server A server B
3---------- ----------
4Table : Test Table : SUBS
5-------------- ---------------
6columns A,B,C Columns D,E,F,G,H
7
8BEGIN TRAN
9
10INSERT LinkedServer.dbo.Dest (Col1, Col2, Col3)
11SELECT Col4, Col5, Col6
12FROM Source S WITH (TABLOCK, HOLDLOCK)
13WHERE NOT EXISTS (
14 SELECT 1
15 FROM LinkedServer.dbo.Dest D WITH (TABLOCK, HOLDLOCK)
16 WHERE S.Key = D.Key
17)
18
19DELETE D
20FROM LinkedServer.dbo.Dest D
21WHERE NOT EXISTS (
22 SELECT 1
23 FROM Source S
24 WHERE D.Key = S.Key
25)
26
27UPDATE D
28SET
29 D.Col1 = S.Col4,
30 D.Col2 = S.Col5,
31 D.Col3 = S.Col6
32FROM
33 LinkedServer.dbo.Dest D
34 INNER JOIN Source S ON D.Key = S.Key
35WHERE
36 D.Col1 <> S.Col4
37 OR Coalesce(D.Col1, '!-NULL-!') = Coalesce(S.Col5, '!-NULL-!') -- or some way to handle nulls if they can be present
38 OR D.Col3 <> S.Col6
39
40COMMIT TRAN