-1

I have an SSIS package that is executing dynamic SQL. The Dynamic SQL will sometimes have result set and sometimes not, but I only want to capture the COUNT, which is the returned value in below script.

(or can I use C# Script task to accomplish this?), I know I can loop through record sets in C#, but I wont always have 2, is there way to tell difference in result sets by name or something, or only by index of array in resultset[x]?

-- These are passed values in SSIS as paramaters
DECLARE @VariablePassedForSelect AS VARCHAR(50) = ?
DECLARE @ScriptToExecute NVARCHAR(MAX) = ?


-- putting return value count from dynamic script into variable
DECLARE @ReturnRowsEffectedA INT

-- this MAY (or may not) return a result set as dynaic code
exec sp_executesql @ScriptToExecute, N'@VariablePassedForSelect BIGINT, @ReturnRowsEffected INT OUTPUT', @VariablePassedForSelect = @VariablePassedForSelect, @ReturnRowsEffected = @ReturnRowsEffectedA OUTPUT

-- now select results for returning,this is only value I want
SELECT @ReturnRowsEffectedA   AS RowCountR

All this works fine in SQL and returns the result set from the dynamic SQL and the count, but I only want the COUNT returned into a variable in SSIS.

If I use result set, I get error for single row (because of the dynamic sql can return results and count).

Three are reasons I am doing it this way and not using SP's or other means, but those details are not required for my question.

How can I only capture just the value for @ReturnRowsEffectedA?

Brad
  • 3,454
  • 3
  • 27
  • 50
  • You should be able to configure `@ReturnRowsEffectedA` as an Output parameter in the `Parameter Mapping` pane of the `Execute SQL Task`. There's a simple example over at https://sqlserverrider.wordpress.com/2014/08/31/execute-stored-procedure-with-input-and-output-parameters-and-return-status-in-ssis/ – digital.aaron Jan 24 '20 at 18:10
  • I tried a few variations of that but the code I am running to return count is not an SP so there is no official RETURN statement, and I cant do a RETURN from a select not in SP (that I Found), and the code above has to run this way without an SP. So the output parameter in SSIS wont work. – Brad Jan 24 '20 at 20:02
  • If you only want the count, couldn't you just change the dynamic sql you build so that it doesn't return the first set of records? – digital.aaron Jan 24 '20 at 20:34

1 Answers1

1

You can do this using an Output parameter. First, create an SSIS variable that will hold the value. I'm going to use the name you already have and call this variable [User::ReturnRowsEffectedA]. You won't need @ReturnRowsEffectedA in the actual query. You can keep most of the query text the same, but replace @ReturnRowsEffectedA in the sp_execute call with ? to denote we're going to use an SSIS parameter variable. You Execute SQL query will look like this:

-- These are passed values in SSIS as paramaters
DECLARE @VariablePassedForSelect AS VARCHAR(50) = ?
DECLARE @ScriptToExecute NVARCHAR(MAX) = ?


-- this MAY (or may not) return a result set as dynaic code
exec sp_executesql @ScriptToExecute, N'@VariablePassedForSelect BIGINT, @ReturnRowsEffected INT OUTPUT', @VariablePassedForSelect = @VariablePassedForSelect, @ReturnRowsEffected = ? OUTPUT

Then in your Parameter Mapping, you will add a third entry (the first two will be your input params that feed @VariablePassedForSelect and @ScriptToExecute). You'll use [User::ReturnRowsEffectedA] as the Variable Name, set Direction to Output, and change Parameter Name to 2. You can leave the datatype set to LONG and the size set to -1.

I created an example on my system to illustrate how this works: Here's my query. I don't pass in the script, for simplicity. But it does still use an input and an output parameter. enter image description here

Here you can see I have ResultSet set to None: enter image description here

The Execute SQL Task only uses one parameter, so the Parameter Name here is 0. enter image description here

I have paused execution after the task has completed so you can see that [User::ResultVar] is populated with the value of 1, which is the expected value here. enter image description here

Edit: To add the results of the proc to an Object-type variable in SSIS, you'd first create an Object-type variable, then you'd set the ResultSet property to Full result set and then map the results to the variable.

enter image description here enter image description here

digital.aaron
  • 5,435
  • 2
  • 24
  • 43
  • This worked perfectly! Thank You. I found this as an option before, but it was not close enough to my scenario so I must not have implemented it correctly, your example is perfect!! Thanks again!! – Brad Jan 27 '20 at 19:41
  • Hey, this worked great for me, but I have an issue now, is there a way to set the paramater that is output to an OBJECT? I am not finding a way to do that. The reason is the output is a string longer than varchar(8000), and it is getting truncated, I know how to resolve that, but I need to output to an object to do so. – Brad Feb 13 '20 at 16:18
  • 1
    @Brad you could change the `ResultSet` value to `Full result set`, and then in the `Result Set` pane, you'd add that an entry that maps `Result Name` of 0 to your object variable. See the screenshots I added to the answer for reference. – digital.aaron Feb 13 '20 at 16:25
  • My code has changed slightly from above, I am not outputting 2 variables with different values, so I think I should be able to remove the output parameters, and use the to object to full result set as you have above, and then in C# code (this part is needed for truncating long strings), loop through result sets and save them to the correct variables? I can search how to do this in c#, but would this work even if one resultset if null/blank? it would still be a result set, correct – Brad Feb 13 '20 at 16:43
  • 1
    That is correct. If your proc outputs a NULL/blank recordset, that recordset should be present in the ADO object variable. – digital.aaron Feb 13 '20 at 16:51