I have two tables, a master table and a relation table. The Master table as an automatic identity ID field (DOCUMENT_ID) that is generated upon insert. The relation table ties that DOCUMENT_ID from the master table with a USER_NAME entry. I want to be able to create a copy of the the record in the master table and a copy of its relation in the relationship table while establishing the new relation using the new automatically generated DOCUMENT_ID generated by the Master table. Here is an example of my tables and the desired output.
MASTER TABLE:
DOCUMENT_ID | DOCUMENT_NAME
1 | Application
2 | Invoice
3 | Receipt
RELATION TABLE:
DOCUMENT_ID| USER_NAME
1 | John
1 | Amy
2 | Jim
2 | Jane
3 | John
3 | Jane
I would like to copy the records from the master table and create a copy of the relationship so that my output looks like this:
MASTER TABLE WITH NEW RECORDS INSERTED: Note that the DOCUMENT_ID column generated the IDs automatically.
DOCUMENT_ID | DOCUMENT_NAME
1 | Application
2 | Invoice
3 | Receipt
7 | Application
8 | Invoice
9 | Receipt
RELATION TABLE: This tables needs to tie the relation between the new IDs created in the master table and a copy of the USER_NAMES that were associated with the original DOCUMENT_IDs.
DOCUMENT_ID| USER_NAME
1 | John
1 | Amy
2 | Jim
2 | Jane
3 | John
3 | Jane
7 | John
7 | Amy
8 | Jim
8 | Jane
9 | John
9 | Jane