i'd like to have a stored procedure which either performs a bulk insert, bulk update or does nothing from a table on one server (CTS_CUSTOMERS - ServerA) to another on a different server (CTS_CUSTOMERS - ServerB)
firstly if the record does not exist on serverB then insert it from serverA, something like below ?
if it does already exist, then compare the last time the record was updated between the two database tables (column name is called last_update) and if it is different then run an update.
else do nothing !
if i run this every day I suppose i only need it to loop round records on ServerA where the last_update column is >= today's date.
how can i fit it all together including including the loop round ?
thanks
firstly if the record does not exist on serverB then insert it from serverA, something like below ?
Code:
INSERT INTO serverB.databaseB.CTS_CUSTOMERS (CustID) SELECT CustID FROM serverA.databaseA.CTS_CUSTOMERS AS S WHERE NOT EXISTS (SELECT CustID FROM serverB.databaseB.CTS_CUSTOMERS WHERE serverB.databaseB.CustID = S.CustID)
if it does already exist, then compare the last time the record was updated between the two database tables (column name is called last_update) and if it is different then run an update.
Code:
update serverB.databaseB.CTS_CUSTOMERS
set serverB.databaseB.CTS_CUSTOMERS.CustID = serverA.databaseA.CTS_CUSTOMERS.CustID
serverB.databaseB.CTS_CUSTOMERS.last_update = serverA.databaseA.CTS_CUSTOMERS.last_update
from
serverB.databaseB.CTS_CUSTOMERS d1
inner join serverA.databaseA.CTS_CUSTOMERS d2
on d1.CustID= d2.CustID
where d1.last_update <> d2.last_update
else do nothing !
if i run this every day I suppose i only need it to loop round records on ServerA where the last_update column is >= today's date.
how can i fit it all together including including the loop round ?
thanks