Not Exists in SSIS
Tuesday, March 3, 2009 at 7:03PM There are many times that I would like to insert a record into a table only if it doesn't exist. This is done easily in T-SQL:
insertTableA
selectB.RepID
fromTableBB
wherenotexists(select1
fromTableAA
whereA.RepID=B.RepID)
SSISis a little more complex, but once you get the hang of it, it's very powerful.
Create adataflowobject and within it place adataflowsource. Select from theSSIStoolbox, underdataflowtransformations, theLookuptask. Attach your task to thedataflowsource. Once you have that connected we move on to the next part.
Double-click on theLookuptask and it will open up a window with 3 tabs. First tab is the reference table, that is the table you will be checking. In the example above, that would beTableA. I personally like to select only the columns I need to validate what I am inserting (which would most likely be the 1 or more columns that make up the unique key). Once this is done, click on theColumnstab.
TheColumnsis where you link the reference table, (Table B)to the source table (TableAin the above example). Drag one or more columns from theAvailable InputColumson the left to the correspondingAvailableLookupColumnson the right. Click any one of the check boxes from the right side. It doesn't matter which one as we are only using this as a flag. Once you do this, you will see an entry in the grid below withLookupColumn,LookupOperation,Output Alias. This should be filled in with the column on the left, the corresponding column you clicked thecheckboxon to the right and in the middle it should say <add as new column>. Remember this column name. We aregoing to skip theAdvancedtab (I'll cover that in another post soon). Now, at the very bottom left of theLookuptask you will see a buttonConfigure Error Output, click on it.
Across the top you will see 5 columns, you want to focus on the thirdError. Right below that column you will see, by default,Fail Componentand if you click in it you will have 3 options:gnoreFailure, Redirect Row, Fail Component. You want to selectIgnore Failure. You might be asking why.
The purpose of theLookuptask is to find an existing record and if no record matches it is considered a failure. We alter that default behavior and tellSSISto pass the record through even if there is no match. We catch the failure with aConditional Splittask.
The output of theLookuptask goes into theConditional Split. Drag down theISNull()function and place it on the first line. Click on theColumnsfolder on the left side and find the column we selected up above in theLookuptab. Drag that column into theISNullfunction. I usually change the output name to something along the lines ofValid_RecordorNew_Record. What you have done here is basically tell theConditional Splittask to output any record with that has a null value, which translates into: Pass any record that doesn't already exist. You then connect the output of that condition to your next task and continue on programming. There you have it, usingSSISto input records that don't already exist.
SSIS,
existence check,
lookup,
transformation
Josef Richberg 
