June 20, 2012 at 3:08 pm
Hi all,
I have a table A, which has data from Jan - June (YTD). I want to update the june data daily from an excel file based on 2 fields (Report Date and Name), then add new rows if they don't exist already.
Can someone completely guide me through on how to do this and the sql necessary to create this output?
Much appreciated,
Alan
June 20, 2012 at 11:10 pm
Sounds like you want to:
1. Create an SSIS package to import your Excel on the schedule.
2. You can UPDATE existing rows and INSERT new rows (colloquially called an UpSert) using a MERGE statement in the SSIS package.
More details from you, like table DDL, Excel format, some sample data, etc. would be needed from you to provide more specifics.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
June 21, 2012 at 8:25 am
You can use SSIS package or Import/Export in SQL Server Management Studio to import Excel file into a temp table such as TMP_YTD table. Then build a query to update and insert date
For example
UPDATE a
SET TotalAmount = ytd.TotalAmount
FROM TableA a
INNER JOIN TMP_YTD ytd ON a.ReportDate = ytd.ReportDate
INSERT INTO TableA
SELECT ytd.ReportDate,ytd.Name,ytd.TotalAmount
FROM TMP_YTD ytd
LEFT JOIN TableA a ON ytd.ReportDate = a.ReportDate
WHERE a.ReportDate is null
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply