How can I retrieve the identities of rows that were inserted through insert…select?

后端 未结 4 1529
悲哀的现实
悲哀的现实 2020-12-29 16:42

I am inserting records through a query similar to this one:

insert into tbl_xyz select field1 from tbl_abc

Now I would like to retreive th

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 17:13

    You can get this information using the OUTPUT clause.

    You can output your information to a temp target table or view.

    Here's an example:

    DECLARE @InsertedIDs TABLE (ID bigint)
    INSERT into DestTable (col1, col2, col3, col4)
    OUTPUT INSERTED.ID INTO @InsertedIDs
    SELECT col1, col2, col3, col4 FROM SourceTable
    

    You can then query the table InsertedIDs for your inserted IDs.

提交回复
热议问题