SSIS How to UPDATE instead of INSERT (UPSERT)

I'm working on several SSIS Transfer Packages right now and I just figured out how to do an "UPSERT": How to insert rows that don't exist in the destination table and how to update rows that exist in both source and destination tables. This will work across servers (which is the environment I'm working on).

This is how one table/view would look like in an SSIS package:

Yes, that means (as far as I know) that you have to create four objects for every table you want to transfer data from/to.

The Source object is, of course, connected to the Source database's table/view. The other objects beneath it (the Lookup, Destination and Update objects) are all connected to the Destination database's table/view.

The Update object is the only tricky element in this package. I specify the update script manually, which looks like:
UPDATE dbo.MyTable
SET Column1 = ?
,Column2 = ?
,Column3 = ?
WHERE ID= ?
(see: Generate SSIS Update script which I use to generate the update script for each table that I have in my SSIS package)

Each "?" will be automatically mapped by the system based on the order you defined it. All you have to do next is to specify the Column Mappings. Make sure that you do it in the right order like the image below:


Note how the ID is specified last -- that's because its corresponding "?" was specified last in the Update statement.

4 comments:

Jinyoung said...

Ack!

UPDATE dbo.MyTable SET
Column1 = ?
,Column2 = ?
,Column3 = ?
WHERE ID= ?

RiVuC said...

How can I put names to the parameters?

ashuthinks said...

HI AFTER LOOKUP I HAVE USED OLEDB DESTINATION AND DID MAPPING LIKE BELOW http://social.microsoft.com/Forums/getfile/19170/

IS THERE ANY NEED OF UPDATE QUERY ???

oyen said...

Hi, ashuthinks,

You only need the update query if you want to update existing data. If all you want is to insert missing data, then you don't need it at all.