Welcome to EMC Consulting Blogs Sign in | Join | Help

SSIS Junkie

Once upon a time this blog was a hive of activity. Now however its pretty lifeless as you can probably tell so if are pining for more of the same you can find me over at http://sqlblog.com/blogs/jamie_thomson. I look forward to seeing you there!

SSIS Nugget: Execute SQL Task into an object variable - Shred it with a Foreach loop

Here is the first in what will hopefully be a long series of postings in what I have colloquially called my SSIS Nuggets series.

I was reading a post this morning from Sam Bendayan in which he was pondering how to store a full result set returned from the Execute SQL Task. He had followed BOL (which according to Sam states that a Full Result Set should be stored in a variable of type String) but was getting errors.

Well, that's not surprising really. BOL is wrong! A Full Result Set needs to be stored in variable of type Object. Typically you would then navigate through (sometimes called shred) this result set using a Foreach Loop (using the "Foreach ADO Enumerator"), carrying out whatever operations you wanted to on the way. In the example I have put together I simply output the contents of the current row of the result set using the familar message box.

Here's a screenshot of this package:

20050704_ExecuteSQLTask_nugget1.JPG

Here's everything that the package does:

  • Populates a variable called User::FullResultSet using the Execute SQL Task. The data comes from the AdventureWorks.Production.Culture table (so you will need to have the AdventureWorks DB installed)
  • Shreds the recordset using the Foreach loop and populates 2 variables: User::CultureID & User::Name with values from the current row of the result set
  • Outputs the values of User::CultureID & User::Name in a message box

Yes yes, I know that Kirk, Allan & Darren, and myself have all previously posted similar examples of doing this, but one more can't hurt right?

You can download the package from here. All you need to do to run it is edit the connection manager to point at your SQL Server instance.

-Jamie

 

Published 04 July 2005 09:55 by jamie.thomson

Comments

 

Douglas Laudenschlager said:

We have located a misleading statement on this subject in Books Online and correcting it promptly. The column values of a Single row resultset are all returned as strings. An XML resultset can be returned as a string or as an object. A Full resultset is return as an object. Please encourage your readers regularly to use the "Send Feedback" link available on every page of BOL! Your comments quickly reach the right person and contribute to better documentation. Thanks, -Doug
July 5, 2005 21:39
 

Peter said:

This is exactly what I was looking for. I just couldn't figure out how to link a recordset from my query into a ForEach loop. Having the package to reference helped immensely. I was trying to go through the private newsgroups and even searched @ SQLIS.com without any luck on using a recordset as opposed to looping through files.

Thanks for the article.
August 30, 2005 19:00
 

Nice blog said:

Very useful blog. Thank you.
February 22, 2006 19:34
 

Saul said:

Jamie - Good article, but do you know how when you run a SProc in Mgmt Studio, you get a ResultSet + a "Message"?
How do you capture this "Message" text into an SSIS variable (either Object or String type)?
Doesn't matter whether it's via ODBC or OLE DB or Native SQL connection, as long as I can get the text out somehow.
THNX
March 7, 2006 04:09
 

John Hoge said:

>>We have located a misleading statement ... The column values of a Single row resultset are all returned as strings <<

Ah-ha! it's still in BOL no wonder I was getting cast errors with numerics.
Thank you for the sanity check.
March 23, 2006 17:00
 

jamie.thomson said:

John,
As you can see, Doug's comment above was posted in July 2005. I'm disappointed that hasn't been corrected in BOL.
Could you use the Send Feedback link in BOL to inform them of the problem.

Thanks
Jamie
March 23, 2006 17:04
 

Mafti said:

i'm having trouble getting it running.

the error is from the foreach-container.

it cannot convert somehow the index-0 (which is an identity) to a string, int32,int64.

it just tells me the types are different.

February 23, 2007 08:52
 

John said:

This is great, but it doesn't work for an MDX resultset.  

Even thought my resultset has only one row, it requires an object variable to output to.  But then the looping container tries to match the forst row, which I can only assume is a header row.  

Is there any way to 'skip' headers?

February 26, 2007 14:18
 

Mark said:

Good stuff!!!

July 19, 2007 19:52
 

Bob said:

Awesome.  This was very helpful.

July 26, 2007 00:20
 

Tom said:

Excellent.  I'm just getting into SSIS development, and your blog has repeatedly saved me hours.  The BOL is sorely lacking in "How-To" with regards to tasks like this, and you do a great job filling in the blanks!

August 3, 2007 15:46
 

SQL Junkie said:

Can we output the resultset to a file (instead of having a message box).

I was successful in doing it but had to open the file, write, close. Lot more IO.

Any simpler way or elegant way of doing it.

August 17, 2007 16:03
 

SQL Junkie said:

Can we output the resultset to a file (instead of having a message box).

I was successful in doing it but had to open the file, write, close. Lot more IO.

Any simpler way or elegant way of doing it.

August 17, 2007 16:04
 

jamie.thomson said:

SQL Junkie,

If you want to output the content of the recordset to a file then can you not simply take the SQL statement and oput it ito a data-flow?

-Jamie

August 17, 2007 17:17
 

David said:

Yes, you can output the query results using a data-flow.  I have a follow-up question, is there a quick way to append those results to an existing file?  I'm trying to send query results in the body of an email and would like a header in the message body before the results are listed.

August 27, 2007 18:29
 

AWM_dB said:

Hi Jamie,

I tried to follow the above example exactly but still I am getting this error

Error: Variable "User::ADOVar" does not contain a valid data object

August 28, 2007 01:34
 

Biju said:

I have found this code ujseful and many thanks!!..Is there a way to select folders (not files)  with the foreach loop . I need to select and delete folders that are older than a specified date. I know we can delete with the file task. How do I select the folder checking the date and pass it to the file task???

Would you happen to have any ideas?

Thanks again!!

September 14, 2007 11:16
 

oyun said:

If you are building asynchronous custom components for SSIS then at some point you will need to use the various Set

thanks

September 24, 2007 21:46
 

Adam Dyler said:

Hi, is there a way to then chuck the results of an SQL Task into an Email (Send Email Task) and send it?

Thanks for the useful post!

October 10, 2007 17:58
 

SSIS Junkie said:

I&#39;ve just been made aware that as a result of a recent migration of our blog infrastructure some

November 13, 2007 21:49
 

sumit said:

i have tried this one but getting the error as mentiond by User::rsDetails does not contain a valid data object.Could anyone guide me for this?

November 19, 2007 06:16
 

Bharat said:

Same error as above (Valid data obj)

ny solution experts ??????

December 12, 2007 09:37
 

jack555 said:

For the not valid data object error, make sure you have set correct variable in the  foreach loop container settings in Collection tab at "ADO object source variable". And that it's not empty at run time. Such mistake caused this kind of error in my case.

January 8, 2008 14:51
 

evden eve nakliyat said:

Thanksss

February 16, 2008 23:07
 

rubyA said:

Hello Jamie,

         I want to move data from 5 tables in database1 to corresponding 5 tables in database2 and in a single package.How to do that?

May 9, 2008 11:26
 

jamie.thomson said:

rubyA

Build a package with 5 data flows in it.

Have I misunderstood? What's the problem?

-Jamie

May 9, 2008 12:42
 

Thy said:

Real nice post.

Greatly apreciated

May 16, 2008 12:27
 

Thy said:

Real nice post.

Greatly apreciated

May 16, 2008 12:27
 

Thy said:

Real nice post.

Greatly apreciated

May 16, 2008 12:27
 

morten said:

Is this the only way to get a value from a resulotset?

I dont want to make a for each loop because I only retrive a single row.

Is it possible to get a value from the resultset without having to use the for each?

Maybe something like User::FullResultSet::0 ???

June 12, 2008 10:16
 

jamie.thomson said:

morten,

The Execute SQL Task allows you to specify that you're only returning a single row of data and you can then push each value in that row into a variable.

-Jamie

June 12, 2008 10:20
 

morten said:

How is that push done?

June 12, 2008 12:13
 

jamie.thomson said:

Its all in Books Online or alternatively try here: http://www.sqlis.com/58.aspx (about halfway down where it talks about ResultSet='Single Row'

-Jamie

June 12, 2008 12:20
 

Mukti said:

Hi,

I am using ADO enumerator for the first time in Foreach loop. It’s very similar to example shown above. I am using Execute SQL Task with OLEDB connection (connection to SQL Server 2000) to process the shredded recordset. I have properly mapped the Parameters in Execute SQL task to the Foreach variables. Now when I use the parameter in the sql command, something like,

UPDATE Person

SET Email = NULL

WHERE PersonID = ?

Throws following error:

<Query text> failed with the following error: "Syntax error, permission violation, or other nonspecific error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

The interesting thing is if I change the Execute SQL Task connection to ADO.Net connection and change the query syntax to .net compatible, everything runs fine. I am not sure what’s going on with OLE DB connection.

The way I understand is this: The full recordset returned by the query before the foreach loop stores the recordset in Ado.net by default and then when I try to use it in Execute SQL task via parameters there is some datatype mismatch.

Am I right in thinking this way? Just curious about it as i spent lot of time fixing this problem.

Thanks,

Mukti

July 23, 2008 01:03
 

Sesli27 said:

Good stuff!!!

July 25, 2008 16:48
 

Sesli27 said:

Good stuff!!!

July 25, 2008 16:48
 

oyunlar said:

thanks blogs.conchango.........

August 5, 2008 12:28
 

molla said:

thanks

August 28, 2008 07:26
 

coco said:

thanksss

August 28, 2008 07:27
 

tylersoun said:

Jamie,

Instead of looping through one by one and waiting for each to finish before iterating and setting the next connection, i want to set the connection and execute my Execute Sql task one by one without having to wait for each Execute sql task to finish.  I guess i'm trying to execute my Execute SQL task Asynchronous within my Foreach Loop.  Is this something that can be done within ssis?  If so, can you head me in the right direction?

thanks.

November 11, 2008 19:53
 

jamie.thomson said:

tylersoun,

Let me restate what you just said to make sure I understand. You want to execute an Execute SQL Task (or many Execute SQL Tasks) upon each iteration but you don't want the loop to wait for the completion of the Execute SQL Task(s) before starting the next iteration. That's how I would interpret your use of the word "asynchronous".

Is that what you mean? If so then the answer is, no, you can't do that.

-Jamie

November 12, 2008 09:24
 

Michael Webb said:

Hey I have down this Example.

My issue is I need to replace a field in the SQL with a value from a Variable I have already set in the package.

Here is what I have.

I have Execute SQL Task that runs pulls back 6 records each having and update statement. I need to execute each update statement but before I do I need to replace the @CycleName in the SQL update statement ("Update @CycleName ").

Having issue for one I think its an object.

hope that made since

please email me reply mxwebb@msn.com

November 13, 2008 20:05
 

reklam said:

thank youu

November 21, 2008 18:26
New Comments to this post are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems