How would you migrate SOME records from a header-detail pair of tables (with IDENTITY ID's) to a new pair of tables in another DB?
For example, yo need to migrate record numbered 4,6,9 and 10 from the header table and all their details.
When you INSERT the header records on the new table, their ID's will be 1,2,3 and 4. When inserting the details, the child record from 4 will need to point to 1, 6 to 2 and so on.
Is there a way to use the OUTPUT clause to include a field that is not inserted in the table, as a way to pair "old" ID' to "new" ones?
The
OUTPUTclause of anINSERTstatement will only allow you to select values that are actually inserted into the target table. You can work around this by using aMERGEinstead, as theOUTPUTclause of aMERGEstatement will allow you to select values from the source data as well. Here's an example:Results of the
MERGEstatement: