我对MERGE语句有疑问。我正在比较2张桌子。 当“ MATCHED UPDATE”工作时就像“魅力”一样,但是“ WHEN NOT MATCHED THEN INSERT”子句存在问题。 我想将行插入到daSRC(源)表中,但我需要具有与源表中相同的da_DocId。
这是简化的查询。
MERGE DocAnalytical daSRC
USING (
select
da_DaId, da_DocId, da_DelId,
FROM DocAnalytical dad
JOIN DocElems del on dad.da_DelID=del.Del_ID
) as dest
ON dest.da_DaId=daSRC.da_DaId --i know it's identity but normal its more complex clause
WHEN MATCHED THEN
UPDATE SET
da_DocId=dadSRC.da_DocId, da_DelId=dadSRC.da_DelId
WHEN NOT MATCHED THEN
INSERT (da_DocId, da_DelId,
VALUES(daSRC.da_DocId, da_DelId, -- daSRC not available
);
所需结果: 之前
da_DaID da_DocId da_DelId da_DaId (identity)
----------- ----------- -----------
162 5160 98
163 5160 98
164 5160 99
不匹配的行
da_DaID da_DocId da_DelId
----------- ----------- -----------
300 5300 100
之后
da_DaID da_DocId da_DelId
----------- ----------- -----------
162 5160 98
163 5160 98
164 5160 99
165 5160 100 << dad_DocId from daSRC (source)