我想通过insert和select语句使用现有数据来克隆表中的数据,如下所示:
INSERT INTO Foundation.Products
OUTPUT Inserted.Id INTO @insertedIdTable -- Need to get the existing Id also
SELECT Name, SupplierId, CategoryId
FROM Foundation.Products
WHERE Id = @existingProductId有没有办法在表中映射插入的Id和现有的Id。为哪个现有Id创建新Id?
我知道我们可以使用MERGE语句,将Source和Destination作为同一个表
MERGE Foundation.Products
USING (SELECT * FROM Foundation.Products WHERE Id = @existingProductId) AS [Source]
ON (1=0) -- always insert
WHEN NOT MATCHED THEN
INSERT (Name, SupplierId, CategoryId)
VALUES ([Source].Name, [Source].SupplierId,[Source].CategoryId)
OUTPUT INSERTED.Id, [Source].Id; 发布于 2020-07-05 14:22:36
不,你想要的东西不能用insert - output来完成。您必须使用merge。你不想使用merge有什么原因吗?
https://stackoverflow.com/questions/62734173
复制相似问题