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

后端 未结 4 1543
悲哀的现实
悲哀的现实 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:29

    As far as I know, you can't really do this with straight SQL in the same script. But you could create an INSERT trigger. Now, I hate triggers, but it's one way of doing it.

    Depending on what you are trying to do, you might want to insert the rows into a temp table or table variable first, and deal with the result set that way. Hopefully, there is a unique column that you can link to.

    You could also lock the table, get the max key, insert your rows, and then get your max key again and do a range.

    Trigger:

    --Use the Inserted table.  This conaints all of the inserted rows.
    SELECT * FROM Inserted
    

    Temp Table:

    insert field1, unique_col into #temp from tbl_abc
    
    insert into tbl_xyz (field1, unique_col) select field1, unique_col from tbl_abc
    
    --This could be an update, or a cursor, or whatever you want to do
    SELECT * FROM tbl_xyz WHERE EXISTS (SELECT top 1 unique_col FROM #temp WHERE unique_col = tbl_xyz.unique_col)
    

    Key Range:

    Declare @minkey as int, @maxkey as int
    
    BEGIN TRANS --You have to lock the table for this to work
    
      --key is the name of your identity column
      SELECT @minkey = MAX(key) FROM tbl_xyz
      insert into tbl_xyz select field1 from tbl_abc
      SELECT @maxkey = MAX(key) FROM tbl_xyz
    
    COMMIT Trans
    
    SELECT * FROM tbl_xyz WHERE key BETWEEN @minkey and @maxkey
    

提交回复
热议问题