Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a SQL Server 2k8 with a couple of columns in few tables populated with predefined data. The predefined data is published by a 3rd party as MDB/XLS/CSV files periodically. I need to apply these updates to the corresponding columns in the tables. A simple example would be having tables for city, state and zip, which gets populated using data from a 3rd party.

To update the tables periodically, i want to run incremental updates by determining the delta and updating only those. I am thinking of writing a windows app to do this.

Are there any tools in SQL Server that allows me to do the delta between two tables and update only the delta onto the target? Does SSIS help in any way?

thanks for reading!

share|improve this question
add comment

1 Answer

Yes! SQL 2008 introduced exactly that, the Merge command.

Technet MERGE (Transact-SQL)

Here is an example from the above article:

    USE AdventureWorks;
GO
-- Create a temporary table variable to hold the output actions.
DECLARE @SummaryOfChanges TABLE(Change VARCHAR(20));

MERGE INTO Sales.SalesReason AS Target
USING (VALUES ('Recommendation','Other'), ('Review', 'Marketing'), ('Internet', 'Promotion'))
       AS Source (NewName, NewReasonType)
ON Target.Name = Source.NewName
WHEN MATCHED THEN
    UPDATE SET ReasonType = Source.NewReasonType
WHEN NOT MATCHED BY TARGET THEN
    INSERT (Name, ReasonType) VALUES (NewName, NewReasonType)
OUTPUT $action INTO @SummaryOfChanges;

-- Query the results of the table variable.
SELECT Change, COUNT(*) AS CountPerChange
FROM @SummaryOfChanges
GROUP BY Change;

If you are not interested in inserting records that originate form your external source and are not present in your internal data then remove:

WHEN NOT MATCHED BY TARGET THEN
    INSERT (Name, ReasonType) VALUES (NewName, NewReasonType)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.