views:

21

answers:

2

SQL Server 2008.

Morning,

If someone could offer their advice it would be very much appreciated.

Currently I’m looking to import two XML files, Donation amount and Donation Maker.

The Donation Amount file would comprise of an Amount field and a DonationMakerID, and the Donation Maker file would comprise of a Name field and DonationMakerID. The Donation Maker would make the donation of a certain amount brought back in the Amount file.

My issue is I will occasionally receive a Donation Amount file with a Donation Maker ID that doesn’t relate to any Donation makers currently in the database but will probably relate to a Donation MakerID in the Donation Maker XML file which I will receive later that day.

Now what would be the best way to store the Donation Amount information until I receive the Donation Maker file? I could obviously not load the Donation amount file until I have a matching Donation MakerID but I would rather load the file as and when I get them.

Ideally I need a FK constraint that only enforces referential integrity in the Donation Amount table for IDs that already exist in the Donation Maker table…

I have a few ideas for example, two columns in dbo.DonationAmount of DonationMakerID and XMLDonationMakerID where DonationMakerID can allow NULLs. Then when I receive a Donation Amount file I load the DonationMakerID into the XMLDonationMakerID field and if there is a matching ID in the maker table I load that to the Donation makerID column otherwise I leave it NULL. Then when I receive subsequent Donation Maker XML files I can check the XMLDonationMakerID field against the ID in the Donationmaker XML file and if I find one that matches update DonationMakerID.

Hopefully that all makes sense.

Many thanks

+1  A: 

Could you possibly

  • detect when a DomainMakerID is not yet present
  • if so: add an new entry to your DomainMaker table, and flag it as "temporary" or "provisional" or whatever
  • once you get the DomainMaker with that new ID, update the row and remove the "temporary" flag

Something like that should work (I hope!) and allow you to insert your data from the XMLs.

marc_s
@Marc_S.Many thanks for the response.I've gone with something fairly similar, when I import the DonationDetail file I import the ID given to me to XMLDonationDetailID regardless and the amount to the amount field.I also LEFT JOIN the Donation Maker table, if the ID matches I update the DonationDetailID to be the relevent ID.So if I dont currently have the donation maker in the system DonationID is null and XMLDonationID is populated with the missingID. Then when I receive the next Donation Maker XML file I check to see if XMLDonationID exists and if so update the DonationID in DonationAmount.
FairFunk
+1  A: 

First, can you wait until you receive the second file and process them in the correct order? Or can you get them to send them at the same time or the donor file first? Fixing the timing issue is the first, best choice.

If that is not possible, move the bad records out to a separate table so you can maintain the foreign key constraints (It is really important to not get rid of the constraints.) Then check that table after the second import and add any records after the correct donor is in. Whatever you do, you do not want to put the data into the production table until the associated data is with it or you will have meaningless garbage in your database. And getting rid of a necessary FK constraint in order to do so is a guarantee of other problesm with data integrity in the future.

HLGEM
@HLGEM, Cheers for the reply. After reading your advice I've redesigned and gone with your recommendation to add a staging table to put Donation Amounts without the Associated Donation Maker. Then when i load the next Donation Maker file I check for the relevent IDs and Delete, insert, update etc . Your meaningless garbage statement struck a cord as I dont think I’m treating some data with enough respect.
FairFunk