合并:当与源不匹配时 - 更新行
Merge: when not matched by source - update rows
我正在尝试找出合并函数,我必须更新目标 table 中存在的行,但这些行与源 table 中的行不匹配。
那些在源中不匹配的 table 我想在行中更新它说的 WHEN NOT MATCHED BY SOURCE with something like
Update PEOPLE set UPD = null,target.CHANGE = CURRENT_TIMESTAMP where target.ID = source.ID and target.UPD is not null and target.CHANGE is null
这就是 MERGE 目前的样子,我想我需要一些输出来给我不匹配的 ID,这样我就可以更新它们,但不确定如何更新。
例如,这是 PEOPLE 中的行:
ID 不是唯一的,它是组的 ID
ID,NAME,SURNAME,UPD,CHECKED
4345,JOHN,DOE,1 - this one doesn't exist(his ID doesn't exist) in
'#PEOPLE because it is insert from an earlier merge from an earlier #PEOPLE that now has changed
879,MARY,HON,1 - this one exist in #PEOPLE
9875,CHRISTIAN,TROY,1 - this one doesn't match the row's but his ID exist
in PEOPLE and #PEOPLE
因此,从这个列表中,我希望 JOHN DOE 保持原样,因为他的 ID 不会在#PEOPLE 中退出,并且 CHRISTIAN TROY 得到更新,其中 UPD 将为 NULL 且 CHANGE = CURRENT_TIMESTAMP 但前提是UPD 不为空,CHANGE 为空,因为他的 ID 存在于 PEOPLE 和 #PEOPLE 中,但整行不匹配。
MERGE INTO PEOPLE WITH (HOLDLOCK) AS target
USING #PEOPLE AS source
on isnull(target.ID,'') = isnull(source.ID,'')
and isnull(target.NAME,'') = isnull(source.NAME,'')
and isnull(target.SURNAME,'') = isnull(source.SURNAME,'')
WHEN MATCHED THEN
UPDATE SET target.UPD = 1
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID,NAME,SURNAME,UPD)
VALUES (source.ID ,source.NAME ,source.SURNAME,1)
WHEN NOT MATCHED BY SOURCE and target.UPD is not null and target.CHANGE is null THEN
update set UPD = NULL,target.CHANGE = CURRENT_TIMESTAMP
有什么想法吗?
阅读所有这些内容有点困难,但据我所知,您的 table 中没有任何 FK,这就是为什么您必须在合并后进行更新。
您可以使用 OUTPUT,但最简单的方法可能是像这样进行更新。如果匹配 UPD=2 如果匹配并插入 UPD = 3.. 所以现在你在 UPD = 1 上有那些不匹配的所以你看到#People 中的 ID 是什么然后更新它们:
MERGE INTO PEOPLE WITH (HOLDLOCK) AS target
USING #PEOPLE AS source
on isnull(target.ID,'') = isnull(source.ID,'')
and isnull(target.NAME,'') = isnull(source.NAME,'')
and isnull(target.SURNAME,'') = isnull(source.SURNAME,'')
WHEN MATCHED THEN
UPDATE SET target.UPD = 2
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID,NAME,SURNAME,UPD)
VALUES (source.ID ,source.NAME ,source.SURNAME,3)
;
UPDATE PEOPLE set UPD = null,CHANGE = CURRENT_TIMESTAMP where UPD = 1 and CHANGE is null and ID in (Select distinct ID from #PEOPLE);
UPDATE PEOPLE set UPD = 1 where (UPD =2 or UPD=3) and MB in (Select distinct MB from #PEOPLE);
我正在尝试找出合并函数,我必须更新目标 table 中存在的行,但这些行与源 table 中的行不匹配。
那些在源中不匹配的 table 我想在行中更新它说的 WHEN NOT MATCHED BY SOURCE with something like Update PEOPLE set UPD = null,target.CHANGE = CURRENT_TIMESTAMP where target.ID = source.ID and target.UPD is not null and target.CHANGE is null
这就是 MERGE 目前的样子,我想我需要一些输出来给我不匹配的 ID,这样我就可以更新它们,但不确定如何更新。
例如,这是 PEOPLE 中的行:
ID 不是唯一的,它是组的 ID
ID,NAME,SURNAME,UPD,CHECKED
4345,JOHN,DOE,1 - this one doesn't exist(his ID doesn't exist) in '#PEOPLE because it is insert from an earlier merge from an earlier #PEOPLE that now has changed
879,MARY,HON,1 - this one exist in #PEOPLE
9875,CHRISTIAN,TROY,1 - this one doesn't match the row's but his ID exist in PEOPLE and #PEOPLE
因此,从这个列表中,我希望 JOHN DOE 保持原样,因为他的 ID 不会在#PEOPLE 中退出,并且 CHRISTIAN TROY 得到更新,其中 UPD 将为 NULL 且 CHANGE = CURRENT_TIMESTAMP 但前提是UPD 不为空,CHANGE 为空,因为他的 ID 存在于 PEOPLE 和 #PEOPLE 中,但整行不匹配。
MERGE INTO PEOPLE WITH (HOLDLOCK) AS target
USING #PEOPLE AS source
on isnull(target.ID,'') = isnull(source.ID,'')
and isnull(target.NAME,'') = isnull(source.NAME,'')
and isnull(target.SURNAME,'') = isnull(source.SURNAME,'')
WHEN MATCHED THEN
UPDATE SET target.UPD = 1
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID,NAME,SURNAME,UPD)
VALUES (source.ID ,source.NAME ,source.SURNAME,1)
WHEN NOT MATCHED BY SOURCE and target.UPD is not null and target.CHANGE is null THEN
update set UPD = NULL,target.CHANGE = CURRENT_TIMESTAMP
有什么想法吗?
阅读所有这些内容有点困难,但据我所知,您的 table 中没有任何 FK,这就是为什么您必须在合并后进行更新。
您可以使用 OUTPUT,但最简单的方法可能是像这样进行更新。如果匹配 UPD=2 如果匹配并插入 UPD = 3.. 所以现在你在 UPD = 1 上有那些不匹配的所以你看到#People 中的 ID 是什么然后更新它们:
MERGE INTO PEOPLE WITH (HOLDLOCK) AS target
USING #PEOPLE AS source
on isnull(target.ID,'') = isnull(source.ID,'')
and isnull(target.NAME,'') = isnull(source.NAME,'')
and isnull(target.SURNAME,'') = isnull(source.SURNAME,'')
WHEN MATCHED THEN
UPDATE SET target.UPD = 2
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID,NAME,SURNAME,UPD)
VALUES (source.ID ,source.NAME ,source.SURNAME,3)
;
UPDATE PEOPLE set UPD = null,CHANGE = CURRENT_TIMESTAMP where UPD = 1 and CHANGE is null and ID in (Select distinct ID from #PEOPLE);
UPDATE PEOPLE set UPD = 1 where (UPD =2 or UPD=3) and MB in (Select distinct MB from #PEOPLE);