这对我有用
MERGE Table1 AS tgt USING (SELECT TOP 1 * FROM Table2,(SELECT itmid FROM Table3 WHERE id=@id) as a WHERE id = @id) AS src ON (tgt.id = src.id) WHEN MATCHED THEN UPDATE SET qty = qty + @qty WHEN NOT MATCHED THEN INSERT itmid VALUES itmid;
MERGE Table1 AS tgt USING (SELECT TOP 1 * FROM Table2 WHERE id = @id) AS src ON (tgt.id = src.id) WHEN MATCHED THEN UPDATE SET qty = qty + @qty WHEN NOT MATCHED THEN INSERT itmid SELECT itmid FROM Table3 WHERE id=@id;