我有一个
SQL TABLE Inventory有许多列,其中有两列 LocalSKU(pk)varchar(200)NOT NULL QOH int
和 只有两列的EXCEL DATA LocalSKU和QOH
我想实现一个查询,我希望根据LocalSKU匹配这两个数据,如果它们匹配,查询应该更新QOH。如果他们不匹配,什么都不做。
我已经查询了这个问题
SqlCommand sqlcmd = new SqlCommand(@"MERGE Inventory AS target
USING (select LocalSKU, QOH from @source) as source
ON (source.LocalSKU = target.LocalSKU)
WHEN MATCHED THEN
UPDATE SET QOH = source.QOH
WHEN NOT MATCHED THEN
INSERT (QOH)
VALUES (source.QOH);", sqlconn);
它不起作用。请指导我如何实现这一点。
示例将不胜感激。
答案 0 :(得分:0)
SqlCommand sqlcmd = new SqlCommand(@"MERGE Inventory AS target
USING (select LocalSKU,QOH, Integer3 from @source) as source
ON (source.LocalSKU = target.LocalSKU)
WHEN MATCHED THEN
UPDATE SET Integer3 = source.QOH;", sqlconn);