0

我有这样的合并语句:

MERGE DESTINATION
USING SOURCE WHERE <Some_Conditions>
WHEN MATCHED AND <Some_Conditions> THEN DELETE
WHEN MATCHED UPDATE
WHEN NOT MATCHED INSERT 

这工作正常,但我还有一个条件,即当源中不存在它们的条目时,我必须更新目标中的行。

前任。

来源

Column1 Column2 Column3
-----------------------
A        A       A
B        B       B

目的地

Column1 Column2 Column3
-----------------------------------
B        B       <Some_Calculation>
D        D       <Some_Calculation>

现在,由于源中没有行D,我必须Column 3通过一些计算在目标中进行修改。但是由于合并只给出了三个选项deleteupdate以及insert行匹配与否。

如何在上述语句中实现此功能?

编辑

用我的评论编辑问题:

在上面我给出的示例中,它正在运行,然后它正在更新B并插入A到目标表中。D但是即使源表中不存在我也想更新

4

1 回答 1

0

也许阅读以下文档MERGE

MERGE 
[ TOP ( expression ) [ PERCENT ] ] 
[ INTO ] <target_table> [ WITH ( <merge_hint> ) ] [ [ AS ] table_alias ]
USING <table_source> 
ON <merge_search_condition>
[ WHEN MATCHED [ AND <clause_search_condition> ]
    THEN <merge_matched> ] [ ...n ]
[ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ]
    THEN <merge_not_matched> ]
[ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]
    THEN <merge_matched> ] [ ...n ]
...

WHEN NOT MATCHED BY SOURCE是你需要的

于 2013-01-10T11:16:27.717 回答