SSIS - How to pass datetime variable to stored procedure - sql-server

I wanted to create package in SSIS.
At one of the first steps I would like to set the 'bookmark' variable (data type in SQL is datetime) using SQL Server stored procedure. Than I would like to pass that variable to other stored procedure which would prepare the table to the export.
Unfortunately I got stuck with the issue which I guess is related to datatypes in SSIS, could you please have a look?
So I created an variable time for that:
Than I used execute SQL task which have as SQL Statement:
exec MyProcedure ?,?,? out
And Parameter mapping as per below:
During the execution I can see that it is assigned as an expected (the only thing which is odd at that place is that SSIS uses MM/DD/YYYY format, while I have everywhere YYYY/MM/DD)
After that I have the SQL task which is calling the 2nd procedure:
exec MyProcedure2 #var1 = ?, #var2 = ?, #bookmark =?
And that component is failing all the time with the errors as per below:
[Execute SQL Task] Error: Executing the query "exec MyProcedure2
#var1 = ?, #var2 = ?, ..." failed with the following error: "The type
is not supported.DBTYPE_DBTIME". Possible failure reasons: Problems
with the query, "ResultSet" property not set correctly, parameters not
set correctly, or connection not established correctly.
Please note that the procedure runs without any issues when I trigger that directly from SQL with the same values.
My understanding/guess is that I messed up something with data types in SSIS... but I was trying change DBTIME to DBTIME2, DBTIMESTAMP, DBDATE, DATE.. and each time I was getting similar error messages and at this moment I am out of ideas.

If you're using an OLEDB connection, I think User::time must be type NVARCHAR because it's an input variable. I think it's different for output variables.
https://learn.microsoft.com/en-us/sql/integration-services/control-flow/execute-sql-task?view=sql-server-ver15#use-date-and-time-parameters-with-ole-db-connection-managers

Related

Execute SQL Task output parameter vs ResultSet

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

Issues when using SSIS Execute SQL Task to insert a row into SQL Server with multiple parameters

I am trying to use SSIS to insert a row with multiple parameters but I get this error :
[Execute SQL Task] Error: Executing the query "insert into ids (id,sqljobname,ojobname,ojobstartd..." 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.
This is how I set up the Execute SQL Task:
insert into ids (id, sqljobname, ojobname, ojobstartdate)
values (?, ?, ?, ?)
Please let me know where I set it up wrong. I don't think the ParentJobStartDate is an issue because I set it up with string.
I am sorry guys, everything I did was correct. I just made a mistake when creating the table in sql server. I created the table like this:
create table ids
(id int,
sqljobname nvarchar(255),
ojobname nvarchar(255),
ojobstartdate nvarchar(255),
sqljobstartdate datetime )
So the columns will only have 1 for the size and make the insert failed. I have already fixed it.

Execute Stored Procedure with multiple result sets

I am using SSIS 2016. I need to execute a stored procedure that returns 4 result sets. I only need to keep the first result set and write this to a table. I can not modify the stored procedure. I do not care about any of the data returned in the other result sets. The stored procedure is in a SQL Server 2016 database. Results will also reside in SQL Server 2016.
I currently have this process running in SSIS 2008 using the "SQL Command" data access mode in an OLE DB Source like below. I have this in a For Each Loop Container to pass a series of param values to the stored procedure as I execute it multiple times for different param values on a daily basis.
SET FMTONLY OFF;
EXEC myProc
#Param1 = ?,
#Param2 =?,
#Param3 = ?;
By default SSIS 2008 is only returning the first result set, which has worked for me as I only care about the first result set.
I am using the Native OLEDB SQL Server client. From what I have read, it has changed the way it handles multiple result sets. I have used the WITH RESULT SETS to define the first result set but if I execute SSIS will fail indicating other result sets need to be defined.
In short, what is the best approach to duplicate what works in SSIS 2008 in SSIS 2016?
Solution Overview
I made 2 Experiments on that issue, the first experiments showed that in case of stored procedures with no parameters, nothing changed in SQL Server 2016 and SSIS 2016, the first Result Set is returned and others are ignored.
The second experiment showed that when using parameters, this will throw an exception, so you have to define metadata using WITH RESULT SETS option, then remove this option.
Detailed Solution
Experiment 1
The following experiment are made using SQL Server 2016 and Visual Studio 2015 with SSDT 2016
First i created this stored procedure
CREATE PROCEDURE sp_Test
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 10 PersonType,NameStyle,Title
FROM [AdventureWorks2016CTP3].[Person].[Person]
SELECT TOP 10 PersonType,Firstname,Lastname
FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
GO
Then i added a Data flow task to SSIS package
Added an OLEDB Source, Recordset destination
In OLEDB source i select the Data access mode to SQL command
an use the following commnad
EXEC sp_Test
When clicking on Columns Tab it shows the first ResultSet structure
And we i executed the package it runs succesfully
Experiment 2
I changed the stored procedures to the following:
ALTER PROCEDURE [dbo].[sp_Test]
#param1 varchar(10),
#param2 varchar(10),
#param3 varchar(10)
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 10 PersonType,NameStyle,Title ,#param2 as 'Param'
FROM [AdventureWorks2016CTP3].[Person].[Person]
SELECT TOP 10 PersonType,Firstname,Lastname,#param3 as 'Param'
FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
And i used the following SQL Command in the OLEDB Source:
EXEC sp_Test ?,?,?
WITH RESULT SETS (
(
PersonType NVarchar(10),
NameStyle NVarchar(10),
Title NVarchar(10),
Param Varchar(10)
)
)
And i mapped the parameters correctly.
When running the package it throws the following exception.
[OLE DB Source 2] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E14.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80040E14 Description: "EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), and the statement tried to send more result sets than this.".
After that i tried to remove the With RESULT SETS option, so the command is :
EXEC sp_Test ?,?,?
I tried to execute the package again, so it is executed with no errors.
Conclusion
Try to use the WITH RESULT SETs option to define the OLEDB Source metadata, after that the metadata is defined, just remove this option and run the package, so it will just take the first Result Set succesfully.
I know it's not exactly what you are asking for, but maybe you could create another stored proc that will return only the first result set for you? (Without touching the first stored proc.) Or insert the data into a table for you and then you just read the data?

Error "The batch could not be analyzed because of compile errors in ssis" in execute sql task while passing with output parameter for stored procedure

I am using sp_send_dbmail to get the mailitem_id as output. When I run the query with output parameter
it's throwing below error and without output it's working fine.
Error: Execute SQL Task] Error: Executing the query "exec ? = sp_send_dbmail #profile_name=?, #recipien..." failed with the following error:
The batch could not be analyzed because of compile errors.
Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Query:
exec ? = sp_send_dbmail #profile_name = ?, #recipients = ?,
#copy_recipients = ?, #subject = ?,
#body = ?, #importance = ?,
#body_format = ?#mailitem_id = ? OUT
#mailitem_id is OUTPUT parameter in the stored procedure, and of INT type..
I tried different methods to pass output parameter but still getting same error... Is it failing with the datatype mismatch or syntax error with passing output parameter unable to get it..
Please help me to resolve the issue..I have added the parameter mapping window in the screen shot. I have tried with Short, ULarge_integer and Large_integer types also instead of Long in parameter mapping for mailitem_id.
Could be one of several things described in the error message, or all of them. I'll work through the error message in the order it is presented.
Resolving the error message
Problems with the query
When using the EXEC #return_status = ... syntax, the T-SQL variable must be defined with the # symbol. The variable must also be declared, so EXEC ? would only be logical if parameter 0 is passed in as a value such as #return_value.
As marc_s commented, the script is missing a comma between two parameters: #body_format = ?#mailitem_id.
#mailitem_id = ? OUT is not the correct syntax for this an output parameter, unless the parameter position for ? is defined as a variable declared within the script (similar to issue #1).
The query should probably look something like this:
DECLARE #return_status INT, #mailitem_id INT;
EXEC #return_status = sp_send_dbmail #profile_name = ?, #recipients = ?,
#copy_recipients = ?, #subject = ?,
#body = ?, #importance = ?,
#body_format = ?, #mailitem_id = #mailitem_id OUTPUT
SELECT #return_status AS return_status, #mailitem_id AS mailitem_id;
For some examples of executing this stored procedure, see the sp_send_dbmail documentation.
"ResultSet" property not set correctly
In the General tab of the Execute SQL Task Editor, what is the value of ResultSet in the Result Set group?
The options are:
None - the query returns no results
Single row - the query returns only one row
Full result set - the query returns multiple rows
XML - the query returns a result set in XML format
Ensure the selected option coincides with how your script is providing the result set and the rest of the task configuration is configured to handle it.
parameters not set correctly
The Result Set tab on the left-hand side of the Execute SQL Task Editor is where parameters from the query should be mapped to SSIS variables. Ensure the mapped variables you are expecting are mapped here according to the ResultSet you chose in the General tab.
Putting it all together
Using the code I provided under "Problems with the query," you would then set the ResultSet property in the General tab of the Execute SQL Task Editor to be Single row. Then, in the Result Set tab, you would map the column values returned from the script (return_status and mailitem_id) to SSIS variables (e.g. User::Status and User::mailitem_id as shown in your screenshot).
Now it's working fine with below query and getting mailitem_id as output.
exec sp_send_dbmail #profile_name=?,#recipients=?,#copy_recipients=?,#subject=?,#body=?,#importance=?,#body_format=?,#mailitem_id= ? OUTPUT

How to use parameters in a SQL Task in SSIS?

I'm working on a datawarehouse.
I wrote a script to loada dimention table by creating date entries (such as the SSAS wizard but directly in my SSIS ETL process). It works well in SSMS and directly in a T-SQL task in SSIS without using parameters).
This script doesn't provide any ResultSet, this is just a loop to insert data in a table.
Here is a quick look of my query.
USE [MySQLServerDatabase]
GO
-- Some parameters used by the script.
DECLARE #PREFIX_YEAR_NAME [nvarchar](50) = 'Year ';
DECLARE #PREFIX_QUARTER_NAME [nvarchar](50) = 'Q';
DECLARE #PREFIX_WEEK_NAME [nvarchar](50) = 'W';
DECLARE #PREFIX_MONTH_NAME [nvarchar](50) = 'M';
DECLARE #DefaultBeginDate [datetime] = '01/01/2000';
DECLARE #Date [datetime];
SET #Date = #BeginDate
WHILE #Date <= #EndDate
BEGIN
-- ...
-- INSERT INTO ...
END
-- ...
However, to get something more easy to use and to maintain, I would like to use SSIS variables directly in the script.
Here is my params (Project.params file).
My script needs all of them to work :
Then, I added a "Execute SQL Task component" containing my SQL query (OLEDB connection, direct input method). I created my 5 parameters :
How to use parameters in the SQL query ?
I tried to use the same name as the variables in the script, it doesn't work.
I tried to use indexes (0,1,2 etc.) names and to use '?' in the script, I doesn't work.
Here is the error when using '?' as parameters :
Error: 0xC002F210 at Execute SQL Task, Execute SQL Task: Executing the
query " DECLARE #PREFIX_YEAR_NAME nvarchar = ?; D..." failed
with the following error: "Multiple-step OLE DB operation generated
errors. Check each OLE DB status value, if available. No work was
done.". Possible failure reasons: Problems with the query, "ResultSet"
property not set correctly, parameters not set correctly, or
connection not established correctly. Task failed: Execute SQL Task
Any idea to solve that ?
Thanks.
you can put this query into stored procedure. In SQL Task Editor in SQLStatement you can execute it by exec [sch].[sp_name] ?,?,?...,? (each ? is one parameter).
In your 'Parameter Mapping' tab in Parameter Name column you can use numbers 0, 1, 2... n (it's order of params in your stored procedure).

Resources