Well, I think you will need to define other criteria for creating a map like oldPK => newPK (for example: Name field is equal?
You can then identify the new PC that matches the old PC and adjust the ParentID .
You can also do a little trick: add a new column to the original table1, which records the new PK value for the copied record. Then you can easily copy the values ​​of Table2 by pointing them to the value of the new column instead of the old PK.
EDIT . I am trying to provide an example code of what I meant by my little trick. I do not change the original database structure, but now I am using a temporary table.
OK, you can try the following:
1) Create a temporary table containing the values ​​of the old table, plus it will get a new PK:
CREATE TABLE
2) Paste all the values ​​from the old table into the temporary table, calculating the new PK, copying the old PK:
INSERT INTO
3) Copy the values ​​to a new table
INSERT INTO NewTable1 SELECT newPKField as ParentId, Name FROM
4) Copy the values ​​from table 2 to NewTable2
INSERT INTO NewTable2 SELECT ChildID, t.newPKField AS ParentId, Foo FROM Table2 INNER JOIN
It has to be done. Please note that this is only T-SQL pseudo-code - I have not tested this in a real database! However, it should come close to what you need.
Thorsten dittmar
source share