表1
-----------------------------
Id | Batch | Qty
-----------------------------
1 A1 5
2 A2 5
3 A3 5
4 A4 5
表2
-----------------------------
Id | Batch | Qty
------------------- ----------
1 A1 6
2 A2 6
3 A3 6
5 A5 10
预期结果
-----------------------------
Id | Batch | Qty
-----------------------------
1 A1 6 (Qty updated)
2 A2 6 (Qty updated)
3 A3 6 (Qty updated)
4 A4 5 (remains as same)
5 A5 10 (row in table 2)
如何在SQL Server中实现此目标?如果有人知道此数据表操作,请共享。。
最佳答案
您可以使用MERGE查询根据表2更新表1:
MERGE INTO Table1 as target
USING Table2 as source
ON (target.id = source.id)
WHEN MATCHED THEN
UPDATE SET target.Batch = source.Batch,
target.Qty = source.Qty
WHEN NOT MATCHED THEN
INSERT (Id, Batch, Qty)
VALUES (source.Id, source.Batch, source.Qty)
关于sql - 如何使用SQL Server中的存储过程合并第三张表中的两个数据表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11046686/