简体   繁体   English

使用输出捕获自动增量标识以及过去的标识映射表

[英]Using output to capture auto increment identity, plus past identity mapping table

I am trying to make exact copies of data in SQL, with new clientIDs, but keep the existing data in the old client as well. 我正在尝试使用新的clientID在SQL中精确复制数据,但也将现有数据保留在旧客户端中。 I will be inserting the data into a table with an auto incrementing integer primary key ID. 我将使用自动递增的整数主键ID将数据插入表中。 I need to retain the ID's of the Old records and the new records together so I can continue using this mapping as I copy the different table data so I can maintain relationships. 我需要将旧记录和新记录的ID保留在一起,以便在复制不同的表数据时可以继续使用此映射,以便维护关系。

At this point I have the following: 此时,我有以下内容:

INSERT INTO [dbo].[Driver]
OUTPUT inserted.ID
       inserted.Name
       inserted.ClientID
SELECT Name,
       1234 AS clientID
FROM dbo.Driver

I am wondering if there is a way to also select the old ID of the driver in the Output so I can then insert all of this into a holding table using the OUTPUT. 我想知道是否有一种方法也可以在输出中选择驱动程序的旧ID,以便随后可以使用OUTPUT将所有这些ID插入保存表中。 So I need to end up with the following after I perform the insert into the dbo.Driver table so I can also insert these values into a temp table: 因此,在将插入到dbo.Driver表中之后,我需要结束以下操作,以便也可以将这些值插入到临时表中:

NewID
OldID
Name
ClientID

At this point I don't know of a way to pull the Original ID from the original record. 在这一点上,我不知道从原始记录中提取原始ID的方法。

you can try... 你可以试试...

INSERT INTO dbo.Driver (oldID, Name, clientID)
SELECT
   B.ID,
   A.Name,
   1234 AS clientID
FROM dbo.Driver A
LEFT JOIN dbo.Driver B ON A.Name = B.Name AND A.clientID = b.clientID

or maybe just 也许只是

INSERT INTO dbo.Driver (oldID, Name, clientID)
SELECT
    ID,
    Name,
    1234 AS clientID
FROM dbo.Drive

我最终按照以下SO帖子使用MERGE INTO来跟踪旧ID: 如何使用OUTPUT捕获新旧ID?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM