In SSIS (sql server 2008) I have a Sql task which is calling my stored procedure.
The stored procedure gets 1 input parameter and return 2 output parameters.
This is the prototype of my SP:
declare spGetPersonDetails(personid int, #orders xml output, #names xml output )
as.....
The problem is that in my sql task in iis, i declared these 3 parameters. one input and 2 outputs, and when it's running only one of the output parameters gets a value from the sp.
Any idea?
Thanks.
I tried recreating this simply with a sample stored procedure.
Stored Procedure
CREATE PROCEDURE [dbo].[TestProcedure]
-- Add the parameters for the stored procedure here
#Input INT,
#Output1 INT OUTPUT,
#Output2 INT OUTPUT
AS
BEGIN
SET #Output1 = #Input + 1
SET #Output2 = #Input + 2
END
Then on the SQL task (which I used an OLE DB source) I had the sql statement set to
EXEC dbo.TestProcedure #Input = ?, #Output1 = ? OUTPUT, #Output2 = ? OUTPUT
Lastly all that was needed is valid parameter mapping ensuring the "Direction" column was set accurately.
I got most of this information from two sites:
http://blogs.msdn.com/b/mattm/archive/2006/11/08/stored-procedures-with-output-parameters.aspx
http://www.julian-kuiters.id.au/article.php/ssis-execute-sql-task-output-parameters
I hope this helps you, if you have more information with the specific values you have set on this SQL task I will be happy to update my answer.
Related
I am converting an Oracle Stored Procedure where the Stored Procedure is returning two INPUT parameters at the same time to my application.
Is it possible to return two OUT Parameters with values set in the stored procedure back to the application using SQL Server Stored Procedures.
My SQL SP is working fine, but I have only been able to return one of the two input parameters.
If it is possible can someone post an example?
Thanks.
SQL Server supports multiple OUTPUT parameters, just define them as OUTPUT in both in the procedure and your call to it:
CREATE PROC dbo.SomeProc #In int, #Out1 int OUTPUT, #Out2 varchar(10) OUTPUT AS
BEGIN
SELECT #Out1 = #In + 1,
#Out2 = CONCAT(#In, 1);
END;
GO
DECLARE #In int = 7;
DECLARE #O1 int, #O2 varchar(10);
EXEC dbo.SomeProc #In, #O1 OUTPUT, #O2 OUTPUT;
SELECT #O1, #O2;
GO
db<>fiddle
I've a stored procedure which outputs a bit datatype. I'm trying to map the output of the stored procedure to a variable in the SSIS package (#myVar)
I'm running this from within a SSIS package and I'm trying to map the datatypes but I can't seem to figure out how to do this.
Output from Stored Procedure = bit
SSIS variable type #myVar = boolean
Mapped parameter = what??
There is no bit or bool in the tasks drop down menu, the closest is variant_bool which doesn't work, and I can't change the output type of the stored procedure -
as it is somebody's else's code.
Any Ideas, anyone?
Test code
create PROCEDURE dbo.uspGetTrueOrFalse
(
#CustomerID int,
#CustomerName nvarchar(101) output,
#IsItTrueOrFalse bit output
)
AS
BEGIN
SET NOCOUNT ON;
SET #CustomerName = 'Red Bicycle Company';
SET #IsItTrueOrFalse = 1; --Set to true
END;
GO
Setup SQL Tasks
Parameter mapping
Execution status of variables
I have a stored procedure that can NOT be modified, the result of this stored procedure is normal select statement as following :
CREATE PROCEDURE LockedProcedure
AS
BEGIN
SELECT * FROM COLORS_TABLE
END
my problem is that I need to get its result as XML result like how the select statement returns when you provide "FOR XML" but without modifying the procedure itself, maybe we can create another stored procedure to call that or user defined function.
This is an example of the procedure that we CAN NOT modify because it is locked.
how to get its result as XML result NOT XML FILE...I don't want any physical file on hard disk.
Thanks.
Solution 1)
Create a temp table matching the output definition of stored procedure.
Use INSERT INTO #Tmp EXEC SPName to insert the stored procedure results into the temp table.
Use FOR XML in combination with SELECT command to fetch the results as xml from temp table.
Solution 2)
Create a CLR User-Defined function to execute the stored procedure and use the BCL facilities to convert the results to xml.
I had a similar problem to this but in my case editing the stored procedure is possible. Even though the OP mentions the stored procedure is locked, I still wanted to post this solution here for others that stumble upon it. This solution assumes the stored procedure uses dynamic SQL to select some data, but could just as easily be adapted to a non-dynamic SQL case.
Add a parameter to the sp such as "#lp_ReturnAsXML BIT = 0". When set to true, you will return the result set as XML using "FOR XML RAW" or some similar command.
Then add this to the end of the stored procedure as an alternative for running the dynamic SQL:
IF #lp_ReturnAsXML = 1
BEGIN
DECLARE #l_XML XML
SET #l_SQL = 'SET #l_XML = (SELECT * FROM ( ' +
#l_SQL + '
) d FOR XML RAW)'
EXEC sp_executesql #l_SQL, N'#l_XML XML OUTPUT', #l_XML = #l_XML OUTPUT
SELECT #l_XML
END
Now something like this should work:
DECLARE #table TABLE
(
Results XML
)
INSERT INTO #table
EXEC p_MyStoredProc ..., #lp_ReturnAsXML = 1
Is there a way to cause the result set of a SQL Server stored procedure (or any result set, after the fact) to be encoded in XML format?
I want the result set to be encoded in XML as if the FOR XML RAW clause was used during selection.
However the complex stored procedure logic and its internal SELECT statements should not be modified to return XML because the procedure is used for its standard/non-XML result set most of the time.
Update: Emphasis on the fact I'm looking for an answer in the SQL Server environment - the
results should be returned as if SQL Server has directly encoded them itself, as XML, just like it does when using the built-in XML features like the FOR XML clause.
You would insert the data from the SP into a temp table, then select from that FOR XML
This won't work if the SP itself already does a INSERT .. EXEC SPROC because you cannot nest them
Working examples
use tempdb;
create proc giveme
as
select a = 1, b = GETDATE()
union all
select 2, b = '20100101'
Using INSERT.. EXEC
declare #t table (a int, b datetime)
insert #t
exec giveme
select * from #t for xml raw
Using OPENQUERY
exec sp_addlinkedserver 'localhost'
exec sp_serveroption #server = 'localhost'
,#optname = 'DATA ACCESS'
,#optvalue = 'TRUE'
select *
from openquery(localhost, 'exec tempdb..giveme')
for xml raw
You could try using OPENROWSET in cooperation with FOR XML to do the transformation.
By 'after the fact', do you mean still within the SQL Server environment? Or are you talking about a client program?
Within SQL, you could probably write a sproc that acts as a wrapper for your other sprocs, along these lines. The wrapper sproc would handle the FOR XML work.
In .NET, there are a number of ways to do this.
You can try inserting the result set from the stored procedure into a table variable( or temporary table) and selecting the table rows with the FOR XML clause.
Here is an example:
DECLARE #MyDataTable AS TABLE ( col1 int,...., colN int)
Make sure that the #MyDataTable has the same columns as the stored procedure result set(s).
INSERT INTO #MyDataTable
EXECUTE mysp_GetData #param1=value,....,#paramN;
SELECT * FROM #MyDataTable
FOR XML AUTO
I am migrating data that has to be inserted using stored procedures which already exist. The stored procedures have parameters and a return value (from a select statement) of an id for the row inserted. Within an OLE DB Command in SSIS, I can call the stored procedure passing column values as the parameters and I usually use output parameters on the stored procedure to handle "id" output; but I am unsure how this can be handled with return values when the procedure uses a select to return the id value. Here is an example of what I have used before which works but I need to pick up the value returned from the select:
exec dbo.uspInsertContactAddress
#Address = ?,
#ContactID = ?,
#DeliveryMethodId = ?,
#ID = ? output,
#Version = ? output
The way I found I could do this which was actually quite simple:
exec ? = dbo.StoredProc #param = ?, #param2 = ?
and then a #RETURN_VALUE will appear on the Available Destination Columns
Don't use the variable names in the SqlCommand property, just the question marks and the "OUT" or "OUTPUT" label for the output parameters.
The trick for grabbing the output parameter value is to put a derived column transformation in the pipeline ahead of the OLE DB Command to introduce a column (mapped to an SSIS variable) to capture the procedure result.
See OLEDB Command Transformation And Identity Columns for a good overview with screen caps of how to do this. Also see Trash Destination Adapter for the Trash Destination used in the first link. It's a handy tool to have available for debugging things like this.
I have always used the parameter mapping within the Execute SQL Task with a lot of success. The SQL Statement is "EXEC nameofstoredproc ?, ? OUTPUT", with the question marks specifying the location of the parameters and OUTPUT if the parameter is an output.
You specify the parameters in the mapping with the appropriate variable name, direction (input, output, ReturnValue) and data type. Since your stored proc is returning the data you want via a result set, then specify the direction for the variables to collect the ID and version as ReturnValue. It should work just fine for you.
If the stored procedure returns a resultset, then you need to capture it:
DECLARE #results TABLE (
[ID] INT NOT NULL
)
INSERT #results ([ID])
EXEC dbo.uspInsertContactAddress #Address = ?, #ContactID = ?, #DeliveryMethodId = ?, #ID = ? output, #Version = ? output
SELECT * FROM #results
Note: I used a TABLE variable. You might need to use a temp table depending on your SQL Server version.