views:

399

answers:

3

I am copying some user data from one SqlServer to another. Call them Alpha and Beta. The SSIS package runs on Beta and it gets the rows on Alpha that meet a certain condition. The package then adds the rows to Beta's table. Pretty simple and that works great.

The problem is that I only want to add new rows into Beta. Normally I would just do something simple like....

INSERT INTO BetaPeople
 SELECT * From AlphaPeople
 where ID NOT IN (SELECT ID FROM BetaPeople)

But this doesn't work in an SSIS package. At least I don't know how and that is the point of this question. How would one go about doing this across servers?

+1  A: 

Your example seems simple, looks like you are adding only new people, not looking for changed data in existing records. In this case, store the last ID in the DB.

CREATE TABLE dbo.LAST (RW int, LastID Int)
go
INSERT INTO dbo.LAST (RW, LastID) VALUES (1,0)

Now you can use this to insert the last ID of the row transferred.

UPDATE dbo.LAST SET LastID = @myLastID WHERE RW = 1

When selecting OLEDB source, set data access mode to SQL Command and use

DECLARE @Last int
SET @Last = (SELECT LastID FROM dbo.LAST WHERE RW = 1)
SELECT * FROM AlphaPeople WHERE ID > @Last;

Note, I do assume that you are using ID int IDENTITY for your PK.

If you have to monitor for data changes of existing records, then have the "last changed" column in every table, and store time of the last transfer.

A different technique would involve setting-up a linked server on Beta to Alpha and running your example without using SSIS. I would expect this to be way slower and more resource intensive than the SSIS solution.

 INSERT INTO dbo.BetaPeople
 SELECT * FROM [Alpha].[myDB].[dbo].[AlphaPeople]
 WHERE ID NOT IN (SELECT ID FROM dbo.BetaPeople)
Damir Sudarevic
+1  A: 

This is the classical Delta detection issue. The best solution is to use Change Data Capture with/without SSIS. If what you are looking for is a once in a life time activity, no need to go for SSIS. Use other means such as linked server and compare with existing records.

Faiz
+1  A: 

Simplest method I have used is as follows:

Query alpha in a Source task in a Dataflow and bring in records to the data flow. Perform all needed Transformations. Before writing to the Destination (Beta) perform a lookup matching the ID column from Alpha to those in Beta. Take the error condition from the lookup task and route it into the Destination. This will prompt you to redirect the rows.

William Todd Salzman