By the way, option 1 will have a bigger hit to performance and speed since it has to query the table an extra time and make an update statement for every existing record. Option 2 will be much faster and have better performance since it is a set based solution instead of a row based solution like option 1.
Main Topics
Browse All Topics





by: aaronakinPosted on 2009-03-02 at 19:47:54ID: 23780758
You really have two solutions, and the best option depends on the size of the text file. If the file is small, go with option 1, but if it's large, go with option 2 as it would be more efficient.
1. In your data flow task, add an OLEDB source component that connects to the database that you will be inserting and updating. Have it pull in the ID field from the existing records in the destination table. Connect it to a Merge Join. Have the OLEDB source be the left input and the flat file as the right input to the merge join and make it a Left Outer Join. This will all be before the OLEDB destination component. Include the ID value from the table in the merge join output along with all columns from the flat file. Add a Conditional Split component after the merge join. In the condition of the spilt component, type in ISNULL(ID) == False. The ID column should be from the table. Name the output for this condition (i.e. Exisiting Records). Name the Default Output Name to something like New Records. Connect the New Records output to the OLEDB destination and connect the Existing Records output to a OLEDB Command component. Specify an UPDATE statement in this component to update existing records in the table.
2. In your data flow task, change the destination table to point to a staging table with the same structure as your final destination table. Don't add any constraints or primary keys though. In your control flow, add an Execute SQL Task after the data flow task and write a SQL statement to update all records in the final destination table with matching records from the staging table. Add another Execute SQL Task after that to insert new records from the staging table to the destination table.