我需要使用遠程數據庫(也是同一本地網絡上不同服務器上的SQL Server)中所做的更改來更新SQL Server中的聯繫人數據庫。我無法對遠程數據庫進行任何更改,這是一個商業產品。我使用鏈接服務器連接到遠程數據庫。兩個表格都包含大約200K行。如何有效比較我的數據與遠程數據庫?
我在這一點上的邏輯很簡單:簡化的僞SQL如下]
/* Get IDs of new contacts into local temp table */
Select remote.ID into #NewContactIDs
From Remote.Contacts remote
Left Join Local.Contacts local on remote.ID=local.ID
Where local.ID is null
/* Get IDs of changed contacts */
Select remote.ID into #ChangedContactIDs
From Remote.Contacts remote
Join Local.Contacts local on remote.ID=local.ID
Where local.ModifyDate < remote.ModifyDate
/* Pull down all new or changed contacts */
Select ID, FirstName, LastName, Email, ...
Into #NewOrChangedContacts
From Remote.Contacts remote
Where remote.ID in (
Select ID from #NewContactIDs
union
Select ID from #ChangedContactIDs
)
當然,做這些連接並通過導線比較是我的命。我確定有更好的方法 - 建議?
不是一個壞主意 - 出於好奇,在那裏你會存儲lastCompareTimestamp? – 2010-02-05 17:44:29
可能位於本地數據庫的新表中。可能是過度殺毒,但它給你一個始終可以訪問和輕鬆更新的位置。 – 2010-02-05 18:32:36