SSIS: How to gracefully handle a SQL task when it returns no rows

Hi all

At the moment I have an SSIS 2008 R2 package, with a SQL task that will occasionally return no rows.  When this happens the package throws an error
[Execute SQL Task] Error: Executing the query "exec ssisMyProc ?, ?
" failed with the following error: "The statement has been terminated.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
 FailPackageOnError is set to False.

Question:  Is there a way to handle this scenario without it throwing an error?

Thanks in advance.
Jim
LVL 67
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan McCauleyData and Analytics ManagerCommented:
I've honestly never seen a failure like this just because a result set is empty - normally, it would proceed with the empty result set and just run the rest of the steps. What is actually failing, and can you confirm that it's because there are no rows returned? Can you log it in a step before the failure occurs so you can be sure you're isolating the correct cause?
0
ValentinoVBI ConsultantCommented:
Hi Jim,

Are you sure the SP is really returning an empty rowset?

I've been trying to reproduce this, with the following results:

o empty result set >> no error
o no result set (for instance with an IF statement in the SP and one of the paths just ends without a SELECT statement) >> error

The error that I see is:

Error: 0xC002F210 at Execute SQL Task, Execute SQL Task: Executing the query "exec test ?" failed with the following error: "No disconnected record set is available for the specified SQL statement.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
But that seems to differ a bit from what you're getting.  These tests are done with the ResultSet property set to Full result set, is that what you've got as well?  Ow, and I'm using the ADO provider (SQLNCLI10.1).

V.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
>Are you sure the SP is really returning an empty rowset?
Yes.  When I manually execute the SP with a parameter that I know returns no rows, it successfully returns no rows.

The SQL task populates an object parameter with the results, and is used in the next step For Each loop.

Now when I run this it just moves on to the next statement, as you guys said, and is not throwing an error.   I do not have an explanation why, other than I likely touched a couple of things that made that happen between asking the question and now.

Resultset value is 'Full Result Set'.

The desired behavior is that if it returns no rows, it gracefully exits the package.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
FIGURED IT OUT

Added a variable @iRowCount to count the rows returned in the SQL Task.
Since I couldn't do an ISNULL() on an Object variable, I added a counter in the next For Each loop to incriment @iRowCount
Then exiting the loop I have two paths:  One is the normal path with an 'Expression and Costraint' @iRowCount != 0, and one that goes to an empty container with an 'Expression and Constraint' @iRowCount == 0.
Viola
Assigning points for troubleshooting help.

Thanks guys.
Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ValentinoVBI ConsultantCommented:
Hey Jim,

Interesting workaround.  You could however just have used a Script component to retrieve the number of records from the result set.  Here's how that can be done, in case you're interested:

ADO.NET (using a System.Data.DataSet):

DataSet ds = (DataSet)Dts.Variables["obj"].Value;
MessageBox.Show(ds.Tables[0].Rows.Count.ToString());

Open in new window

OLE DB:

System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
DataTable dt = new DataTable();
da.Fill(dt, Dts.Variables["obj"].Value);
MessageBox.Show(dt.Rows.Count.ToString());

Open in new window


BTW: thanks for the inspiration, I'll be turning this into a blog article this evening! :)
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
<since you're inspired>  

Slight modification:  Since the data pump can insert multiple rows, i threw a row count in there, and am incrimenting that number.

Let me know if you want screen shots.  

And thanks for the script.  VB.NET scripting is not a strength of mine, and i was just forced to start learning C# scripting via this project, so I appreciate any help I can get.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.