I am currently adding some records to a table using the OLE DB Destination
. Each record has an auto-generated Id
field.
I want to use this generated Id
field as a foreign key in some child records.
I thought I would be able to a data flow line from one OLE DB Destination
component to another, but this is not supported.
I would have thought this was a common problem - how do others solve it?
Workarounds
(1) Generate identity values using Script component
Before
DataFlow Task
add anExecute SQL Task
that return theMAX(ID)
from this tableStore the result in a Variable (ex
@[User::MaxID]
) using aSingle Row
ResultSet@[User::MaxID]
asReadOnly
VariableDT_I4
(ex:NewID
)In the Script Editor use the following Code (i used Visual Basic language)
In the
OLEDB Destination
check theKeep identity
option, and Map theNewID
column to the destination identity columnThen you can use the
NewID
column before that data is imported to OLEDB Destination, because the identity values are predicted in this workaround. (you can add Multicast component to duplicate the data flow, if you need to perform another operation in parallel)References
(2) Use staging table
I ended up using the approach described here:
In my case it looked a bit like this: