2012-09-17 119 views
0

這是我的查詢更新與除聲明

SELECT PageVisit_ID,TargetSite_ID FROM [A].Datawarehouse.mi.ctb_PageEvent WITH (NOLOCK) 
EXCEPT 
SELECT PageVisit_ID ,TargetSite_ID FROM [B].Datawarehouse.mi.ctb_PageEvent WITH (NOLOCK) 

從兩個服務器這兩個表。我需要更新targetsite_id[A].Datawarehouse.mi.ctb_PageEvent記錄從[B].Datawarehouse.mi.ctb_PageEvent 只匹配上面的查詢結果。

回答

1

試試這個

DECLARE @SummaryOfChanges TABLE(Change VARCHAR(20)); 

MERGE INTO [A].Datawarehouse.mi.ctb_PageEvent AS Target 
USING (SELECT PageVisit_ID ,TargetSite_ID FROM [B].Datawarehouse.mi.ctb_PageEvent WITH (NOLOCK)) AS SOURCE 
ON Target.TargetSite_ID = Source.TargetSite_ID 
WHEN MATCHED THEN 
    //UPDATE OR do nothing 
WHEN NOT MATCHED BY TARGET THEN 
    //INSERT 
OUTPUT $action INTO @SummaryOfChanges; 

請參考SQL SERVER – Merge Operations – Insert, Update, Delete in Single Execution。我的解決方案就如何做到這一點提供了一個基本的想法。它最初可能無法正常工作。只要調整它一旦你理解了邏輯

+0

,但我需要僅從這個查詢結果集更新記錄(SELECT PageVisit_ID,TargetSite_ID FROM [A] .Datawarehouse.mi.ctb_PageEvent WITH(NOLOCK) EXCEPT SELECT PageVisit_ID,TargetSite_ID FROM [B] .Datawarehouse.mi.ctb_PageEvent WITH(NOLOCK)) –

+0

現在明白了........ –