Do not use on any
shared computer
August 30, 2008 12:12am pdt
 
[x]
Attachment Details

How to import spreadsheet to append rather than to overwrite?

Tags: SQL
I have a SQL Server job which:

1. deletes rows in PRODUCTDUMP table
2. inserts into the PRODUCTDUMP table all rows from excel spreadsheet (see code snippet)
3. removes FK on BASKETITEM table (to avoid conflicts when importing)
4. removes PK on PRODUCT table (to avoid conflicts when importing)
5. deletes rows in PRODUCT table
6. inserts rows from PRODUCTDUMP into PRODUCT table where productid is not NULL
7. turns the FK on BASKETITEM table back on
8. turns the PK on PRODUCT table back on

The PRODUCT table is updated daily by an ASP website - when an item is sold on the e-commerce website, the PRODUCTACTIVE field is changed from 1 to 0.  

Then once a day at 8pm - the database imports an Excel spreadsheet which the administrator has updated with all items sold in the 'bricks and mortar' shop and manually updated with all website sales (ie. PRODUCTACTIVE changed from 1 to 0)

Ideally - I want the Excel spreadsheet to import at more regular intervals than once daily and I just want it to append to the PRODUCT table where fields are different.
So if the PRODUCTDESCRIPTION is different for PRODUCTID 100, for example, then append the PRODUCTDESCRIPTION for PRODUCTID 100 only rather than overwriting the entire database table.  

The exception rule applies to the PRODUCTACTIVE field - because an item can be sold in the 'bricks and mortar' shop and also on the website - we dont want to override the PRODUCTACTIVE field we only need to change the database if the spreadsheet says different EXCEPT if PRODUCTRETURNED field is a 1 then mark PRODUCTACTIVE as 0

For example, currently - Monday morning at 10am, PRODUCTID 578 is sold on the website and the PRODUCTACTIVE is changed from 1 to 0 so its no longer for sale.  Currently, the administrator updates the excel spreadsheet also to show PRODUCTID 578 as inactive and he also marks all the items sold in the shop as inactive so at 8pm when the database import runs it overwrite the PRODUCT table with the administrators version BUT we dont want the database import to overwrite the PRODUCT table anymore as we just want it to append.  

So we created a PRODUCTRETURNED field where the customer could mark either 1 or 0 in there so the database import could make an exception and say only overwrite the PRODUCTACTIVE field if PRODUCTRETURNED = 1 (e.g. PRODUCTID 578 was sold in the shop on Monday so PRODUCTACTIVE = 0 but on Wednesday, the item was returned so the administrator updates the spreadsheet to show PRODUCTID 578 PRODUCTRETURNED = 1 and PRODUCTACTIVE = 1 and because PRODUCTRETURNED = 1 then the database import also overwrites the PRODUCTACTIVE field)

How can I get the database import to append only and take into account my PRODUCTACTIVE exception?
1:
INSERT INTO Productump (productid, itemnumber, productname, productdescription, brand, listprice, shipping, stock, productactive, productreturned, productsale, productsaleprice) SELECT productid, itemnumber, productname, productdescription, brand, listprice, shipping, stock, productactive, productreturned, productsale, productsaleprice FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\spreadsheet.xls;HDR=YES;IMEX=1', 'SELECT * FROM [Products$]')
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Microsoft
Question Asked By: tingleweb
Solution Provided By: frankytee
Participating Experts: 1
Solution Grade: A
Views: 4
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by frankytee

Rank: Master

Expert Comment by frankytee:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by tingleweb
Author Comment by tingleweb:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by frankytee

Rank: Master

Accepted Solution by frankytee:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628