Execute SQL Task output parameter vs ResultSet - sql-server

We have parameter direction as output in parameter binding and at the same time we do have result binding. So if we are having output variable or return type variable which will be available at output, so why do we need Result binding then.

ResultSets and output parameters are not the same, each one of them has it own use:
ResultSets are used to store a result of a Select query: It can be a one or more columns and it can be a single row or a full result set. ResultSets are retrieved as ADO RecordSets and can be stored within variables. In general a RecordSet can be consumed on time.
Output Parameters are used to store some values that can be set any part of the SQL command (not necessary at the end). Parameters have the same concept of a SQL stored procedure parameters. The value can be used several times.
You can have an Execute SQL Task with output parameters and a ResultSet.
Additional Information
SQL Server Performance ResultSet vs Output Parameter vs Return Value
Result Sets in the Execute SQL Task
Map Result Sets to Variables in an Execute SQL Task
Parameters and Return Codes in the Execute SQL Task
Update 1 # 2019-16-08
You can use the output parameter as input parameter in another stored procedure but you have to execute it in a separate Execute SQL Task.
If you need to execute both stored procedures within one Execute SQL Task, then you can use SQL variables as mentioned in the example below:
DECLARE #output VARCHAR(50)
EXEC proc1 #output OUTPUT
EXEC proc2 #output
Update 2 # 2019-19-09
Recently I published a detailed article about this topic on SQL Shack, you can check it on:
Execute SQL Tasks in SSIS: Output Parameters Vs Result Sets

Related

How to use output parameter in SSIS execute sql task from a stored procedure

Using a parameter, #IsSuccess in stored proc. How to use that #IsSuccess parameter in SSIS Execute SQL Task and take that as output from that component to another component?
You should use the following SQL statement in the Execute SQL Task to store the stored procedure output into an SSIS variable:
EXEC mystoredprocedure ? OUTPUT
Then in the Execute SQL Task editor, go to the parameter mapping form and select the SSIS variable you need to store that value.
More details can be found in the following article:
Execute SQL Task in SSIS: Output Parameters vs Result Sets
After storing that value, you should use this variable within the precedence constraints expressions:
Overview of SSIS Precedence Constraints

SSRS Report Using Stored Procedure

I am working with an SSRS Report that uses a stored procedure.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with ALTER PROCEDURE ...
While I can understand the SQL in a stored procedure, I have never used one in an SSRS Report [I only use 'straight' SQL statements].
When I use SQL as my Dataset, I can copy that SQL into SSMS and run it and see the data it returns.
With this stored procedure, how do I execute it in SSMS to see the data it returns? The stored procedure has a sample 'EXEC ...' statement with all the parameters populated ... but when I run that - no data is returned.
The SSRS report runs fine, but I want to be able to work with the stored procedure in SSMS and see the data it is returning. My goal is to be able to run the stored procedure in SSMS and then tweak it.
How do I work with this stored procedure in SSMS so I can look at the output?
If you just want to execute the procedure in SSMS, locate it in the object browser ([DatabaseName]/Programmability/Stored Procedures). RIght-click the procedure and select 'Execute Stored Procedure'
Fill in the parameters and click OK and a script will be generated to run the procedure.
It's a bit overkill but at least everything is there and you can run it whenever you like.
If you want to edit the proc, right-click and choose modify, a new script will be created (the ALTER PROCEDURE script you mentioned). Make changes as required, run the script and that will modify the procedure, then execute the procedure to see the results.
Of course it would be safer to make a copy and edit that, you can also just run the body of the stored proc by commenting out the ALTER PROCEDURE statement until you are happy with it but you may have to declare and variables that are normally passed in as parameters.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with
ALTER PROCEDURE ...
That's the Alter Procedure script. Use this to edit a stored procedure.
In other words, edit the SQL code you want to optimize, then run the whole script to save the changes.
How do I work with this stored procedure in SSMS so I can look at the
output?
In SSMS use the syntax for stored procedures:
EXEC myspname paramter1, parameter2, param...
Where parameter1, parameter2, etc. are the parameters described in the ALTER Procedure script, directly after the ALTER PROCEDURE myspname. Parameters are preceded by the # symbol.
As you type-in the EXEC procedure command pop-up hints should appear describing the parameter.
Without knowing the code to the stored procedure, it could be doing any number of things based on what is passed to it by parameter. A stored procedure can do DDL and DML queries, and does not necessarily have to select anything at all for output.

Can you add to an SSIS object variable, appending?

We have an SSIS package that calls a Stored Proc to populate a variable object (full result set).
This is fine.
Now we have a need to call that same stored proc again but with different parameters. So really two sets of data that we want stored all together in the same variable object.
If I populate it a second time, does it overwrite what was there, or append to it?
I do this
Execute SQL Task
ResultSet = Full result set
SQLStatement - executes the stored procedure etc.
Result Set
Result Name = 0
Variable Name = User::Subscriptions
I want that to stay. But want to have a second Execute SQL Task that does the exact same thing, just executes the same stored proc with a different parameter. And I want the User::Subscription variable to hold the results of the 1st Execute SQL Task plus the 2nd Execute SQL task. Is this possible?
Adding another Execute SQL Task will overwrite first result set, you can achieve this using the following workaround:
In the Execute SQL task create a Temp table, Insert all result sets into it, select all recored from this temp table in your result set:
Your query will look like:
CREATE TABLE #TblTemp(Column1 varchar(50), .....)
INSERT INTO #TblTemp(Column1 varchar(50), .....)
EXEC Stored_Procedure_1
INSERT INTO #TblTemp(Column1 varchar(50), .....)
EXEC Stored_Procedure_2
SELECT * FROM #TblTemp

SQL Server : 2 Stored Procedure 1 for XML & one for data

I have 2 systems that use the same stored procedure.
The problem is that one system needs data as normal select statement inside the stored procedure, while the other needs the data in XML format
I just looking for best practice,
do I have to create 2 stored procedures so each system read from its own stored procedure?
or I create one stored procedurefor normal data and the other stored procedure read data from the first one and fill it in temp table using OPENROWSET?
or add a parameter for data output so if I want data i pass 1 if I want xml I pass 2?
any other suggestions?
Thanks
Depends on the details of the stored procedure but as a rule of thumb I prefer to add a parameter to determine the output type. In this way you will avoid to maintain two procedures and the risk of going out of sync at some point in the future.
I would suggest Two Stored Procedures. as using OPENROWSET might have some performance issues.
When you say you need data in XML format, this query will look very different from the query which returns data in rowset and both queries will have a very different execution plan.
So if you decide to embed this non-XML procedure inside a OPENROWSET query and then try to pull XML OUTPUT from that query might result in a very inefficient query plan.
I would say simply create Two Separate procedures and have two separate Execution plans for each procedure which can possibly result in a better performance.
Another Option that comes to my mind is that you can add a Parameter to your Procedure like #XML_OUTPUT and at run time check the value of Parameter and run respective queries, something like this...
CREATE PROCEDURE Get_DATA
#Param1 DataType,
#Param2 DataType,
#XML_OUTPUT BIT
AS
BEGIN
SET NOCOUNT ON;
IF (#XML_OUTPUT = 1)
BEGIN
SELECT *
FROM TABLE
FOR XML PATH('')..... bla bla
END
ELSE
BEGIN
SELECT *
FROM TABLE ... bla bla
END
END
And at run time if #XML_OUTPUT was set to 1 , the procedure will return XML Data, else it will return non-xml data.

Insert data from stored procedure returning mutiple dataset into a temporary table in SQL Server 2008

My stored procedure returns more than one result set.
I want to get the data returned by the second select statement from the stored procedure and I want to insert that data in a temporary table in SQL Server 2008.
Any idea how to implement this ?
You can't access more than one result set in SQL Server. A couple of options though:
Split the stored procedures so only one result set it returned.
Access the second result set using c#/vb/net data tables.
Create a CLR to access the second result set.
HTH
C

Resources