OLE DB Command Transformation for INSERT statement : get the inserted rows ID in the output
-
Friday, May 11, 2012 2:01 PM
Hi all,
I may ask a stupid question, but I don't know how to do this :
I am inserting rows into a database using an OLE DB Command Transformation in a dataflow. This is the SQL Statement I use :
INSERT INTO table1 (Field1, Field2) VALUES(?, ?)
I mapped the ? with the right params. The rows are well inserted.
What I want is to retrieve the ID (primary key) that has been given by the server as a new column output of the OLE DB Command Transformation. Of course, I could SELECT again the rows, but if possible, I want to output the ids and continue my dataflow.
Is this possible ? Thanks.
Best regards.
Matteo, .NET Developer and System Engineer
All Replies
-
Friday, May 11, 2012 2:09 PMModerator
Hello Matteo,
you are not the first to ask, in general you incorporate it into a SP and then return the value off it using @@IDENTITY then in the Execute SQL Task capture the full resultset.
I will better point you out to a blog post on how to do that in more details: http://bit.ly/IXpeHV
Arthur My Blog

- Proposed As Answer by Eileen ZhaoMicrosoft Contingent Staff, Moderator Tuesday, May 15, 2012 7:15 AM
- Unproposed As Answer by Eileen ZhaoMicrosoft Contingent Staff, Moderator Tuesday, May 15, 2012 7:16 AM
-
Friday, May 11, 2012 2:18 PMUse @@SCOPE_IDENTITY not @@IDENTITY
Chuck
-
Friday, May 11, 2012 2:24 PM
Hi,
Thank you for your response. Actually, I cannot use Stored Procedures, because I cannot change the database structure. I can only use SSIS objects.
Regards.
Matteo, .NET Developer and System Engineer
-
Friday, May 11, 2012 2:26 PMModeratorStill, must be doable, just write the proper code
Arthur My Blog

-
Friday, May 11, 2012 2:27 PM
Hello Matteo,
you are not the first to ask, in general you incorporate it into a SP and then return the value off it using @@IDENTITY then in the Execute SQL Task capture the full resultset.
I will better point you out to a blog post on how to do that in more details: http://bit.ly/IXpeHV
Arthur My Blog

According to the blog, in my case I cannot do that without SELECT the rows again from the server...
Matteo, .NET Developer and System Engineer
-
Friday, May 11, 2012 2:30 PMModeratorDoes this help: http://sqlserverpedia.com/blog/sql-server-bloggers/inserting-records-and-getting-the-identity-in-ssis-part-2/
Arthur My Blog

-
Friday, May 11, 2012 2:32 PM
The only way I can think of would be to use a script transform to execute your query and a SELECT @@SCOPE_IDENITY. Then you could insert the returned value into the stream in a new column.
Chuck
-
Friday, May 11, 2012 2:56 PMThanks for it. That would be great but not for performance. I have many records to insert at each time...
Matteo, .NET Developer and System Engineer
-
Friday, May 11, 2012 3:00 PM
Thanks for it. That would be great but not for performance. I have many records to insert at each time...
Matteo, .NET Developer and System Engineer
Its not going to be any slower than using a OLE DB Command Transformation once per row - you would be doing the same thing in script but have access to a more complete featureset.
Chuck
- Edited by Chuck Pedretti Friday, May 11, 2012 3:00 PM
- Proposed As Answer by Eileen ZhaoMicrosoft Contingent Staff, Moderator Thursday, May 17, 2012 9:10 AM
- Marked As Answer by Eileen ZhaoMicrosoft Contingent Staff, Moderator Friday, May 18, 2012 4:49 AM

