| Author |
Copying Parent & Child Table
|
|
| Dennis Perkins 2005-04-19, 8:23 pm |
| I'm trying to write a script to copy some parent and child
data from withing a table. This would be like trying to
copy and Invoice and the associated line items to a new
invoice.
I've created a script to insert the parent records with a
select query, but I'm not sure how copy the child records
and assign them to the new parents. The only thing that
makes the parent unique is the pointer that is used to link
the child and this is what is changing.
What is the best way to approach this problem?
TIA
Dennis Perkins
| |
| Preston 2005-04-19, 8:23 pm |
| It sounds like you're doing this in a two step process
1. Copy all the headers
2. Copy all the detail
Possible solutions:
1. create another column in the target detail table and insert the original
parentRecordID in to it. Then go back, make the necessary changes and drop
the temporary column.
2. Copy 1 header. Get the parentRecordID (see @@identity in the help file).
Copy all the detail for that one header. A stored procedure would do this
eaasy enough.
I'm sure there are other solutions as well.
| |
| Dennis Perkins 2005-04-19, 8:23 pm |
| Thank Preston,
The second solution was the easiest to implement and works
fine.
Dennis Perkins
|
|
|
|