Relatively simple problem.
Table A has ID int PK, unique Name varchar(500), and cola, colb, etc
Table B has a foreign key to Table A.
So, in the application, we are generating records for both table A and table B into DataTables in memory.
We would be generating thousands of these records on a very large number of “clients”.
Eventually we make the call to store these records. However, records from table A may already exist in the database, so we need to get the primary keys for the records that already exist, and insert the missing ones. Then insert all records for table B with the correct foreign key.
Proposed solution:
I was considering sending an xml document to SQL Server to open as a rowset into TableVarA, update TableVarA with the primary keys for the records that already exist, then insert the missing records and output that to TableVarNew, I then select the Name and primary key from TableVarA union all TableVarNew.
Then in code populate the correct FKs into TableB in memory, and insert all of these records using SqlBulkCopy.
Does this sound like a good solution? And if so, what is the best way to populate the FKs in memory for TableB to match the primary key from the returned DataSet.
Sounds like a plan – but I think the handling of
Table Acan be simpler (a single in-memory table/table variable should be sufficient):TableVarAthat contains all rows forTable AIDfor all existing rows with their ID (should be doable in a single SQL statement)ID) intoTable Aand make a note of theirIDThis could all happen in a single table variable – I don’t see why you need to copy stuff around….
Once you’ve handled your
Table A, as you say, updateTable B‘s foreign keys and bulk insert those rows in one go.What I’m not quite clear on is how
Table BreferencesTable A– you just said it had an FK, but you didn’t specify what column it was on (assuming onID). Then how are your rows fromTable BreferencingTable Afor new rows, that aren’t inserted yet and thus don’t have anIDinTable Ayet?