当不匹配源时,然后在过滤源上和污垢表上合并时合并

问题描述 投票:0回答:1
我有一张具有亲子关系的2张桌子及其“快照”版本:

CREATE TABLE Parent( ParentId int ) CREATE TABLE Child( ChildId int, ParentId int, ColA int, ) CREATE TABLE ParentSnapshot( ParentSnapshotId int, ParentId int ) CREATE TABLE ChildSnaphost( ChildSnapshotId, ParentSnapshotId ChildId int, ParentId int, ColA int, )
在某个点上,父母和子表被复制到父母napshot和childsnaphost:

现在,对于给定的父母napshotids列表,我需要将Childsnaphost与他们的原始数据同步(Child Table)。

我如何写合并陈述,对于给定的列表,父母napshotids将幼稚的姓氏合并到childsnaphost表中:

如果在Childtable中创建了新条目 如果在Childtable中找不到的话,将新的条目删除为Childsnaphost。 Updates匹配条目
  1. 我正在努力编写父母napshotids列表的地方条件
  2. 没有提到在插入的情况下如何处理
  3. ChildSnapshotId
  4. ,为了简单起见,我假设
ChildSnapshotId

作为一个身份列,每行会增加1个。

表中的示例数据:
sql sql-server t-sql merge sql-merge
1个回答
0
投票


childid
parentid

Cola


112211表中的示例数据 Childsnapshotidparentsnapshotid childid
1 100
200 44
400
parentid

Cola


1211期待是ChildChildSnaphostChildIdcolAChildId450表中插入。 400
1 11 1 100
33 1 300 3
44 1 450
33应该被删除,因为它在子表中不存在,因此44应该从表中更新到table和 22应在
输出

Child


Fiddle



Childsnapshotid
parentsnapshotid
childid
parentid
Cola


1

1

11

1
1004122120031441400
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.