I have a sitution where I need to import records from a flat file into several tables, making sure to keep tab on the resulting Primary keys for each row insert, for insertion into the next table.
Example
For each Row in FlatFile_Import
RowID = INSERT Row_ELEMENTS1 INTO TABLEA
PROPID = INSERT ROW_ELEMENTS2 & ROWID INTO TABLEB
ATTRID = INSERT ROW_ELEMENTS3 & PROPID INTO TABLEC
. . . ..
NEXt
I have a staging table, StagingTable filled with data from an excel file. Each row of the data in StagingTable contains records that need to be inserted into various tables(TableA, TableB, and TableC). Now when I insert the columns from each row that go to TableA, I need to retrieve the created/generated primarykey, KeyA Next I select to insert the columns from StagingTable row that should go into TableB, along with KeyA, and get back KeyB and so on and so forth.
How can I best handle this requirement please?
This solution assumes you can Modify the TableA and TableB to add a column.
Load the Data into StagingTable .Staging table should have StageRecordID ( identity).
Then you need to modify the TableA to add a colum StageRecordID. Store the StageRecordId in this column when populating TABLEA.
WHEN populating TableB join the StagingTable with TableA to get RowID of TableA
similarly Store StageRecordID in TableB aswell so that you can join to it When Populating TABLEC
Edit:some example code