|
Following is my scenario where i am trying to do this the best possible way , i tried in SSIS using lookups but dint work out properly (low perfomance) Table Name: Subtransaction Fields : based on this data i need to update the transaction table with millions of records Table Name :MainTransaction Fields: I need to update the records matching on the transaction Now i need to insert the transaction nos that were updated in to a log table LogTable Can some one help me with this please?
(comments are locked)
|
|
You can simply use update from join syntax though with SQL Server 2008 you could also opt to use MERGE instead. The idea is that you create a table variable which will hold the info about the records affected by the transaction, so you can then insert them into the log table after the update is executed. I assume that the TransactionNo is unique in the SubTransaction, because if not then you might end up with updating the single record more than once, in which case some additional measures would be required for the update script to work properly. Here is the script: Hope this helps, Oleg this is brilliant .. Thanks Oleg
Mar 13 '12 at 05:44 PM
aRookieBIdev
@Oleg Netchaev +1. I always prefer this method for such scenarios. But I guess the steps to insert the data in declared table and then in the physical table could easily be eliminated. Instead the OUTPUT could be directly inserted into the Log table. This way the query could gain a better performance.
Mar 14 '12 at 06:18 AM
Usman Butt
@Usman Butt Thank you and yes, not including the table variable in the mix could and would gain a better performance. I just like playing with table variables while the script is not finalized just to confirm that it works properly, specifically when there is a possibility that the join produces more than one match per main record. If, say, there are 2 unique records in the main table each having 3 matches in the sub table, then the number of affected records is still 2, though the update restated as a select using the same join produces 6 records. Which values from the existing combinations are picked depends on many factors so additional measures are then needed to tune the query to produce desired update (always doable). @aRookieBIdev Usman's recommendation makes sense, which means that you might want to consider removing the line declaring a table variable, remove the final insert into LogTable and replace the line reading in the update statement with direct insert into LogTable:
Mar 14 '12 at 02:44 PM
Oleg
(comments are locked)
|


Which version of SQL Server?
I am using SQL Server 2008 R2
What indexes do you have on the tables?
I have an index created on the TransactionNo just to do this update. But pls note the transactionno is a varchar(64) I will be deleting the index after the update.
It is a bit dodgy that you have old customer, new customer in one table and Customer in the other. And you still want to do the update based upon TransactionNo? Is TransactionNo Unique in both?