What columns can be used in OUTPUT INTO clause?

前端 未结 5 958
孤街浪徒
孤街浪徒 2021-02-05 11:45

I\'m trying to build a mapping table to associate the IDs of new rows in a table with those that they\'re copied from. The OUTPUT INTO clause seems perfect for that, but it does

5条回答
  •  庸人自扰
    2021-02-05 12:38

    (MS) If the table being modified is also specified in the FROM clause, any reference to columns in that table must be qualified with the INSERTED or DELETED prefix.

    In your example, you can't use cglobal table in the OUTPUT unless it's INSERTED.column_name or DELETED.column_name:

    INSERT INTO Private.Content 
        (Tag) 
        OUTPUT cglobal.ContentID, INSERTED.ContentID 
        INTO @Inserted (SrcContentID, TgtContentID)
    SELECT Tag
        FROM Private.Content AS cglobal
        INNER JOIN @Missing AS m ON cglobal.ContentID = m.SrcContentID
    

    What worked for me was a simple alias table, like this:

    INSERT INTO con1 
        (Tag) 
        OUTPUT **con2**.ContentID, INSERTED.ContentID 
        INTO @Inserted (SrcContentID, TgtContentID)
    SELECT Tag
        FROM Private.Content con1
        **INNER JOIN Private.Content con2 ON con1.id=con2.id**
        INNER JOIN @Missing AS m ON con1.ContentID = m.SrcContentID
    

提交回复
热议问题