How to insert multiple records and get the identity value?

前端 未结 7 745
暗喜
暗喜 2020-11-27 03:54

I\'m inserting multiple records into a table A from another table B. Is there a way to get the identity value of table A record and update table b record with out doing a cu

7条回答
  •  粉色の甜心
    2020-11-27 04:14

    Reading your question carefully, you just want to update table B based on the new identity values in table A.

    After the insert is finished, just run an update...

    UPDATE B
    SET NewID = A.ID
    FROM B INNER JOIN A
         ON (B.FName = A.Fname AND B.LName = A.LName)
    

    This assumes that the FName / LName combination can be used to key match the records between the tables. If this is not the case, you may need to add extra fields to ensure the records match correctly.

    If you don't have an alternate key that allows you to match the records then it doesn't make sense at all, since the records in table B can't be distinguished from one another.

提交回复
热议问题