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

4

1 回答 1

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 - 合并操作 - 单次执行中的插入、更新、删除。我的解决方案只是给出了如何做到这一点的基本想法。它最初可能无法 100% 工作。一旦你理解了逻辑,只需调整它

于 2012-09-17T15:46:06.067 回答