Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 merging tables (dropping data)

Author  Topic 

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-04-20 : 10:35:50
So, regarding the first part of what I am doing, that was taken care of yesterday - realizing we needed to set a staging table and perform a merge (to update from a bulk insert). Now, it's getting a little trickier and am looking for a way to basically use the updated data and that's it (without deleting more than necessary from the original table).

ex) original table has this:
id,sku,price,stock
1 , 1 , 50 , 10
1 , 2 , 55 , 20 / file 1
----------------
1 , 3 , 60 , 10
1 , 4 , 60 , 0 / file 2

I put a line in because so many products are filled by excel files - about 50 in total. Now, since these excel files can contain 20,000+ products are even as few as 50 (and some repeat across files), this is where I am stumped now.

Lets say file 1 changes to contain 1 product like this:
1 , 1 , 60 , 5
and file 2 changes to 3 products like this:
1 , 3 , 60 , 5
1 , 5 , 50 , 10
1 , 6 , 40 , 5

the final table would need to be updated like this:
1 , 1 , 60 , 5
1 , 3 , 60 , 5
1 , 5 , 50 , 10
1 , 6 , 40 , 5

Without deleting the whole contents of the original table and rebuilding the whole thing, is there even a logically possible way to compare tables to be merged - what if i add a column stating what file it comes from? compare all from file 1 with the table waiting to merged from the new file 1? If anything is not in the new file anymore, delete it?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-20 : 10:49:53
you can do it provided you dump the results from files on to staging table and then using that table do a merge onto your final destination table to do necessary insert/update/delete operations

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -