How do you retrieve the new keys from an INSERT INTO SELECT FROM query?

后端 未结 3 637
梦毁少年i
梦毁少年i 2021-01-15 01:18

Is there a way to retrieve all the keys of the newly inserted records when using an INSERT INTO ... SELECT FROM query?

3条回答
  •  醉梦人生
    2021-01-15 01:45

    DECLARE @MyVar TABLE ( ID int )
    
    INSERT  INTO dbo.TargetTable
        OUTPUT  INSERTED.ID INTO @MyVar
    SELECT * FROM    dbo.SourceTable
    
    SELECT  * FROM    @MyVar
    

提交回复
热议问题