[英]Output clause insert result into temp table along with source id value
I have a table called TreeItemOption
.我有一个名为
TreeItemOption
的表。
I need to insert new rows into TreeItemOption
table from a temp table and save the mapped values in a different temp table我需要将新行从临时表插入到
TreeItemOption
表中,并将映射值保存在不同的临时表中
INSERT INTO ProductSetup.TreeItemOption (TreeItemHeaderId)
OUTPUT INSERTED.TreeItemOptionId, TI.TreeItemOptionId INTO #NewTreeItemOptionIds
SELECT NTI.NewTreeItemHeaderId
FROM #TTreeItem TI
INNER JOIN #NewTreeItemHeaderIds NTI ON NTI.TreeItemHeaderId = TI.TreeItemHeaderId;
I have a #NewTreeItemHeaderIds
table from that table I'm inserting the new TreeItemHeaderId
into TreeItemOption
table and I'm trying output the newly inserted TreeItemOptionId
as well as old TreeItemOptionId
from TTreeItem
table我有一个来自该表的
#NewTreeItemHeaderIds
表 我正在将新的TreeItemHeaderId
插入TreeItemOption
表,我正在尝试 output 新插入的TreeItemOptionId
以及来自TTreeItem
表的旧TreeItemOptionId
But I'm getting the error但我收到了错误
The multi-part identifier "TI.TreeItemOptionId" could not be bound
无法绑定多部分标识符“TI.TreeItemOptionId”
In conventional insert and update commands,only Magic tables can only be used in the output clause.在常规的insert和update命令中,只有Magic tables只能用在output子句中。 Try a merge statement instead if you want the value from your source table to also be mapped.
如果您希望源表中的值也被映射,请尝试使用合并语句。
something like this像这样的
MERGE DestinationTable DEST
USING SourceTable SRC
ON DEST.ID = SRC.ID
WHEN NOT MATCHED THEN
INSERT
(
DestinationColumnName
)
VALUES
(
SRC.SourceColumnName
)OUTPUT inserted.ID,SRC.SOurceID INTO #MyTemp;
One the above has been executed you'll have the records in the Temp table #MyTemp
with the SourceID and the respective DestinationID mapped上面的一个已经执行,你将在临时表
#MyTemp
中获得记录,其中包含 SourceID 和相应的 DestinationID 映射
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.