简体   繁体   English

Output 子句将结果连同源 ID 值插入到临时表中

[英]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.

相关问题 将存储过程的 output 连同参数一起插入到临时表中 - Insert output of stored procedure into temp table along with parameter 将结果插入临时表 mariadb - Insert result into temp table mariadb 将存储过程结果集插入临时表并查询临时表 - Insert stored procedure result set into Temp table and query temp table 如何使用“ WITH AS”子句将结果插入表 - How to insert result to table with “WITH AS” clause 将动态数据透视查询的结果插入到临时表中 - insert result of dynamic pivot query into temp table 如何将存储过程的结果集插入到临时表中并获取Sybase中的输出参数? - How do I insert the result set of a stored procedure into a temp table AND get the output parameters in Sybase? 在临时表中插入具有不同值的数据,然后插入源 - inserting data with different value then source in temp table 无法从SQL Server中的WITH ROWS AS子句插入到临时表中 - Could not insert into temp table from WITH ROWS AS clause in SQL Server 按日期在临时表中插入最后一个非空值 - Insert last not null value in temp table by date 如何在MySQL中从其他数据库插入新表值及其匹配ID - How to insert new table value from different database along with its matching id in MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM