Repeated inserts with Primary key, foreign key

前端 未结 2 1613
天涯浪人
天涯浪人 2021-01-22 13:14

Can anyone tell me how to do repeated multiple inserts on two tables with primary key, foreign key Here\'s what I\'ve done. This is a very snippet of what needs to be done.

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 13:35

    Use the OUTPUT clause

     DECLARE @IDS TABLE (id INT) 
    
     INSERT INTO [dbo].[Table1]
                   ([UserID]  
                   ,[FirstName])
         OUTPUT inserted.id INTO @IDS          
         SELECT 'User1' AS [UserID]
                   ,'FirstName'
         FROM [dbo].[StatusTable]
    
         INSERT INTO [dbo].[Table2]
                    ([AccountID],[Status])
             SELECT Id, 'S' FROM @IDS
    

提交回复
热议问题