兩個網站A,B放在不同的遠程服務器上,各帶一個sql server.物理距離比較遠.
使用同一個後臺進行管理.本來是放在一臺服務器的,昨天移到國外一臺服務器上,部分數據要共享,另外,部分數據需要更新.
比如新的客戶信息,訂單信息要從A複製到B,及客戶在A站上更新的信息要寫回B,B(與整個後臺在一臺服務器上)上數據經過改動後,又要更改A的部分數據.
通過設置複製可以實現,但比較煩瑣,並且一些意外情況不容易控制(因爲並不是數據對傳或更新), 使用web service進行即時更新時比較好的解決辦法. ,但程序改動量太大,時間要求的急,就花了幾個小時幹了件體力活,還好A上要用的表並不多,只有幾十個.
1,建立鏈接服務器(以下代碼中,是最初測試時這麼做的,實際上建過一次就可以了,不用每次都重建並刪除).
2,創建幾個存儲過程(本來一個就可以了,但考濾數據量大小及業務上對數據操作變更的頻率,就建了幾個,分爲5mi,60mi,120mi,1day),來執行數據比對和更新操作.
3,創建幾個對應的job,執行對應的存儲過程.
哪位朋友有好的解決辦法,請不吝賜教
以下代碼,表和字段爲節選.語句中定義的幾個參數亦爲節選,參數值我定期維護和更新,目的很簡單,在聚集索引上過濾不必要操作的數據. 另外,具體的還有事務處理,這裏就略去了.
EXEC sp_addlinkedserver
'VendorDB',
'',
'MSDASQL',
NULL,
NULL,
'DRIVER={SQL Server};SERVER=192.168.1.15;UID=webdev;PWD=web123dev;'
GO
DECLARE @maxCustomerID INT
DECLARE @maxSaleOrderID INT
DECLARE @maxSaleOrderItemID INT
SELECT @maxCustomerID = 0,@maxSaleOrderID = 0,@maxSaleOrderItemID = 0
/*begin update game data for sale*/
--delete the game not exists
DELETE a FROM Game a
WHERE a.GameID NOT IN(SELECT GameID FROM VendorDB.Vcsale.dbo.Game)
--update rows exist
UPDATE a SET a.Name=b.Name,
a.GoldName = b.GoldName,
a.GoldUnit = b.GoldUnit,
a.TradeMode = b.TradeMode,
a.DirectMode = b.DirectMode,
a.Type = b.Type,
a.StockType = b.StockType,
a.SaleStatus = b.SaleStatus
FROM game a
INNER JOIN VendorDB.Vcsale.dbo.Game b
ON a.GameID=b.GameID
AND CHECKSUM(a.Name,a.GoldName,a.GoldUnit,a.TradeMode,a.DirectMode,a.Type,a.StockType,a.SaleStatus)
!=
CHECKSUM(b.Name,b.GoldName,b.GoldUnit,b.TradeMode,b.DirectMode,b.Type,b.StockType,b.SaleStatus)
--insert new rows
INSERT Game
SELECT a.* FROM VendorDB.Vcsale.dbo.Game a
LEFT JOIN Game b
ON a.GameID=b.GameID
WHERE b.GameID IS NULL
/*end update game data for sale*/
/*begin update gameserver data for sale*/
--delete the server not exists
DELETE a FROM GameServer a
WHERE NOT EXISTS(SELECT 1 FROM VendorDB.Vcsale.dbo.GameServer WHERE a.GameServerID = GameServerID)
--update rows exist
UPDATE a SET a.GameID = b.GameID,
a.Name = b.Name,
a.SaleStatus = b.SaleStatus,
a.Price = b.Price,
a.SearchCode = b.SearchCode
FROM GameServer a
INNER JOIN VendorDB.Vcsale.dbo.GameServer b
ON a.GameServerID = b.GameServerID
AND CHECKSUM(a.GameID,a.Name,a.SaleStatus,a.Price,a.SearchCode)
!=
CHECKSUM(b.GameID,b.Name,b.SaleStatus,b.Price,b.SearchCode)
--insert new rows
INSERT GameServer
SELECT a.* FROM VendorDB.Vcsale.dbo.GameServer a
LEFT JOIN GameServer b
ON a.GameServerID = b.GameServerID
WHERE b.GameServerID IS NULL
/*end update gameserver data for sale*/
/*begin update gametext data for sale*/
--delete the gametext not exists
DELETE a FROM GameText a
WHERE a.TextID NOT IN(SELECT TextID FROM VendorDB.Vcsale.dbo.GameText)
--update rows exist
UPDATE a SET a.GameID = b.GameID,
a.Detail = b.Detail,
a.PageTitle = b.PageTitle,
a.GameNotice = b.GameNotice,
a.IsPicGame = b.IsPicGame
FROM GameText a
INNER JOIN VendorDB.Vcsale.dbo.GameText b
ON a.TextID = b.TextID
AND CHECKSUM(a.GameID,a.Detail,a.PageTitle,a.GameNotice,a.IsPicGame)
!=
CHECKSUM(b.GameID,b.Detail,b.PageTitle,b.GameNotice,b.IsPicGame)
--insert new rows
INSERT GameText
SELECT a.* FROM VendorDB.Vcsale.dbo.GameText a
LEFT JOIN GameText b
ON a.TextID = b.TextID
WHERE b.TextID IS NULL
/*end update gametext*/
/*begin update servertext data for sale*/
--delete the servertext not exists
DELETE a FROM ServerText a
WHERE NOT EXISTS(SELECT 1 FROM VendorDB.Vcsale.dbo.ServerText WHERE TextID = a.TextID)
--update rows exist
UPDATE a SET a.ServerID = b.ServerID, a.PageTitle = b.PageTitle
FROM ServerText a
INNER JOIN VendorDB.Vcsale.dbo.ServerText b
ON a.TextID = b.TextID
AND CHECKSUM(a.ServerID,a.PageTitle) != CHECKSUM(b.ServerID,b.PageTitle)
--insert new rows
INSERT ServerText
SELECT a.* FROM VendorDB.Vcsale.dbo.ServerText a
LEFT JOIN ServerText b
ON a.TextID = b.TextID
WHERE b.TextID IS NULL
/*end update serverText*/
/*begin update saletext data for sale*/
--delete the saletext not exists
DELETE a FROM SaleText a
WHERE a.TextID NOT IN(SELECT TextID FROM VendorDB.Vcsale.dbo.SaleText)
--update rows exist
UPDATE a SET a.WithWhat = b.WithWhat,
a.Content = b.Content
FROM SaleText a
INNER JOIN VendorDB.Vcsale.dbo.SaleText b
ON a.TextID = b.TextID
AND CHECKSUM(a.WithWhat,a.Content) != CHECKSUM(b.WithWhat,b.Content)
--insert new rows
INSERT SaleText
SELECT a.* FROM VendorDB.Vcsale.dbo.SaleText a
LEFT JOIN SaleText b
ON a.TextID = b.TextID
WHERE b.TextID IS NULL
/*end update saletext*/
/*begin update tradeaddress data for sale*/
--delete the tradeaddress not exists
DELETE a FROM TradeAddress a
WHERE a.TradeAddressID NOT IN(SELECT TradeAddressID FROM VendorDB.Vcsale.dbo.TradeAddress)
--update row exists
UPDATE a SET a.GameID = b.GameID,a.Address = b.Address
FROM TradeAddress a
INNER JOIN VendorDB.Vcsale.dbo.TradeAddress b
ON a.TradeAddressID = b.TradeAddressID
AND CHECKSUM(a.GameID,a.Address) != CHECKSUM(b.GameID,b.Address)
--insert new rows
INSERT TradeAddress
SELECT a.* FROM VendorDB.Vcsale.dbo.TradeAddress a
LEFT JOIN TradeAddress b
ON a.TradeAddressID = b.TradeAddressID
WHERE b.TradeAddressID IS NULL
/*end update tradeaddress*/
/*begin update systemconfig*/
UPDATE a SET a.SaleSiteInfoWhenClose = b.SaleSiteInfoWhenClose,
a.SaleSiteStatus = b.SaleSiteStatus
FROM SystemConfig a
INNER JOIN VendorDB.Vcsale.dbo.SystemConfig b
ON 1=1 AND CHECKSUM(a.SaleSiteInfoWhenClose,a.SaleSiteStatus)
!=
CHECKSUM(b.SaleSiteInfoWhenClose,b.SaleSiteStatus)
/*end update systemconfig*/
/*begin update saleblock*/
--delete the saleblock not exists
DELETE a FROM SaleBlock a
WHERE NOT EXISTS(SELECT 1 FROM VendorDB.Vcsale.dbo.SaleBlock WHERE SaleBlockID = a.SaleBlockID)
--update rows exist
UPDATE a SET a.GameID = b.GameID,
a.Amount = b.Amount,
a.ChargePercent = b.ChargePercent
FROM SaleBlock a
INNER JOIN VendorDB.Vcsale.dbo.SaleBlock b
ON a.SaleBlockID = b.SaleBlockID
AND CHECKSUM(a.GameID,a.Amount,a.ChargePercent) != CHECKSUM(b.GameID,b.Amount,b.ChargePercent)
--insert new rows
INSERT SaleBlock
SELECT a.* FROM VendorDB.Vcsale.dbo.SaleBlock a
LEFT JOIN SaleBlock b
ON a.SaleBlockID = b.SaleBlockID
WHERE b.SaleBlockID IS NULL
/*end update saleblock*/
/*update customer*/
--write new customer to vendor db
INSERT VendorDB.Vcsale.dbo.Customer
SELECT a.* FROM Customer a
--WHERE a.CustomerID > @maxCustomerID
--AND NOT EXISTS(SELECT 1 FROM FROM VendorDB.Vcsale.dbo WHERE CustomerID = a.CustomerID)
LEFT JOIN VendorDB.Vcsale.dbo.Customer b
ON a.CustomerID = b.CustomerID
WHERE b.CustomerID IS NULL
AND a.CustomerID > @maxCustomerID
--update customer status for sale
UPDATE a SET a.ClassID = b.ClassID,
a.Consume = b.Consume
FROM Customer a
INNER JOIN VendorDB.Vcsale.dbo.Customer b
ON a.CustomerID = b.CustomerID
AND a.MailAccount = b.MailAccount
AND CHECKSUM(a.ClassID,a.Consume) != CHECKSUM(b.ClassID,b.Consume)
--update customer profile for vendor
UPDATE a SET a.Phone = b.Phone,
a.TrueName = b.TrueName,
a.Non_US_Phone = b.Non_US_Phone,
a.Country = b.Country,
a.Area = b.Area
FROM VendorDB.Vcsale.dbo.Customer a
INNER JOIN Customer b
ON a.CustomerID = b.CustomerID
AND a.MailAccount = b.MailAccount
AND CHECKSUM(a.Phone,a.TrueName,a.Non_US_Phone,a.Country,a.Area)
!=
CHECKSUM(b.Phone,b.TrueName,b.Non_US_Phone,b.Country,b.Area)
/*end update customer*/
/*update saleorder*/
--write new saleorder to vendor db
INSERT VendorDB.Vcsale.dbo.SaleOrder
SELECT a.* FROM SaleOrder a
LEFT JOIN VendorDB.Vcsale.dbo.SaleOrder b
ON a.OrderID = b.OrderID AND a.OrderSerial = b.OrderSerial
WHERE b.OrderID IS NULL
AND a.OrderID > @maxSaleOrderID
------
INSERT VendorDB.Vcsale.dbo.SaleOrderItem
SELECT a.* FROM SaleOrderItem a
LEFT JOIN VendorDB.Vcsale.dbo.SaleOrderItem b
ON a.SaleOrderItemID = b.SaleOrderItemID
WHERE b.SaleOrderItemID IS NULL
AND a.SaleOrderItemID > @maxSaleOrderItemID
--update sale order status for sale
UPDATE a SET a.PayTime = b.PayTime,
a.Status = b.Status
FROM SaleOrder a
INNER JOIN VendorDB.Vcsale.dbo.SaleOrder b
ON a.OrderID=b.OrderID AND a.OrderSerial = b.OrderSerial
AND CHECKSUM(a.PayTime,a.Status) != CHECKSUM(b.PayTime,b.Status)
WHERE a.OrderID > @maxSaleOrderID
------
UPDATE a SET a.LeaveAmount = b.LeaveAmount,
a.Status = b.Status
FROM SaleOrderItem a
INNER JOIN VendorDB.Vcsale.dbo.SaleOrderItem b
ON a.SaleOrderItemID = b.SaleOrderItemID
AND a.OrderID = b.OrderID
AND CHECKSUM(a.LeaveAmount,a.Status) != CHECKSUM(b.LeaveAmount,b.Status)
--update sale order item tradetime for vendor
UPDATE a SET a.TradeTime = b.TradeTime
FROM VendorDB.Vcsale.dbo.SaleOrderItem a
INNER JOIN SaleOrderItem b
ON a.SaleOrderItemID = b.SaleOrderItemID
AND a.OrderID = b.OrderID
WHERE a.OrderID>@maxSaleOrderID
/*end update saleorder*/
GO
/*drop romate vendor database server*/
EXEC sp_dropserver 'VendorDB'
GO