T-SQL Output Clause: How to access the old Identity ID

后端 未结 2 1114
走了就别回头了
走了就别回头了 2021-01-11 20:27

I have a T-SQL statement that basically does an insert and OUTPUTs some of the inserted values to a table variable for later processing.

Is there a way for me to sto

相关标签:
2条回答
  • 2021-01-11 21:06

    You'll have to join back @act_map_matrix onto Act to get the "old" value.

    It's simply not available in the INSERT statement

    edit: one hopes that @new_scriptID and "scriptid=2" could be the join column

    0 讨论(0)
  • 2021-01-11 21:08

    I was having your same problem and found a solution at http://sqlblog.com/blogs/adam_machanic/archive/2009/08/24/dr-output-or-how-i-learned-to-stop-worrying-and-love-the-merge.aspx

    Basically it hacks the MERGE command to use that for insert so you can access a source field in the OUTPUT clause that wasn't inserted.

    MERGE INTO people AS tgt
    USING #data AS src ON
        1=0 --Never match
    WHEN NOT MATCHED THEN
        INSERT
        (
            name,
            current_salary
        )
        VALUES
        (
            src.name,
            src.salary
        )
    OUTPUT
        src.input_surrogate,
        inserted.person_id
    INTO #surrogate_map;
    
    0 讨论(0)
提交回复
热议问题