【发布时间】:2014-11-14 14:20:34
【问题描述】:
我正在使用 SQL Server 2008 并在两个表之间同步数据,如下所示:
MERGE into Stock as Target
using StockHistory as Source on Target.SrNo = Source.SrNo
When Matched then
# Update record to target table
When Not Matched then
# Insert record to target table
When Not Matched By Source Then
# Update Source Table - Current Record
问题1:我想有条件地插入记录“当不匹配时”触发。
例如如果源表列refSrNo_StockCompany 为空,则不应插入目标。
问题 2:如果目标表 "Stock" 与源不匹配 "When Not Matched By Source Then" 触发,它应该更新源表 --> IsSoldOut = 'Yes'。
请建议我如何实现这一目标。
谢谢
【问题讨论】:
-
A
MERGE(很像INSERT、UPDATE和DELETE)只能影响 single 表中的行 - 你不能让它更改为Target和Source。 -
只是好奇,在你的代码中,“When Not Matched By Source”条件会被执行吗?因为它是“When Not Matched”的子集,所以它总是先运行它。
-
是 xbb,如果目标不满足给定条件的源,它将触发...
标签: sql-server tsql