How can i use Parameter Mapping in Execute SQL Task in SSIS? - sql-server-2008

I am trying to retrieve the value of Key from a table with a simple select statement in SSIS through Execute SQL Task. But have no luck figuring out this error.
I have used one input variable with string data type and used this variable in parameter mapping in Execute SQL Task.
Executing the query "SELECT cast([Key] as Int) FROM Table where
column = ?" failed with the following error: "An error occurred while
extracting the result into a variable of type (DBTYPE_I4)". Possible
failure reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established
correctly.
Note : Datatype for Key column is tinyint

This message occurs when the default datatype for the parameters remains as 'LONG' instead of whatever is necessary... In your case, this should be 'BYTE'

Tinyint is not i4, it's DT_UI1. http://msdn.microsoft.com/en-us/library/ms345165.aspx
If you change your SSIS type to byte then you should be able to assign the results of your query to the value.
Variable User::input Data Type Byte Value 2
Variable User::output Data Type Byte Value 0
Source query SELECT CAST(1 AS tinyint) AS [key], ? AS foo
Execute SQL Task, OLE DB CM, single row result set
Parameter mapping tab
Variable Name: User::input
Data Type: Byte
Parameter name: 0
Result of column 1 mapped to User::output
Inspect value after Execute SQL Task and result is 2 (expected)

Write the query like -> SELECT cast([Key] as Int) as Key FROM Table where column = ?

Related

SSIS ExecuteSQL task error with ResultSet

Hi I am trying to return a ResultSet from an "Execute SQL Task" in SSIS.
I am then trying to save the result in a "ResultSet" variable.
My Query works in the console and my connection is ok to.
Here is my Query
SELECT Src FROM [myDB].[dbo].[myTable] group by Src
Src is nvarchar(255)
When I execute the task I am getting following error
[Execute SQL Task] Error: Executing the query "SELECT Src
FROM [myDB].[dbo].[myTable..." failed with the following error: "The type of the value (DBNull) being assigned to variable "User::ResultSet" differs from the current variable type (String). Variables may not change type during execution. Variable types are strict, except for variables of type Object.
". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I am also attaching the screen shots for the Task setup.
Please help.
ExecuteSQL Screen1
ExecuteSQL Screen2 - ResultSet setup
In SSIS 2012 within a ForEach loop using an ADO enumerator, you are unable to assign a null value to a variable of type String when the source value came from a SQL query returning a null value.
I would suggest Derived Column Transform feature of SSIS
you'll use the SSIS ISNULL function to detect the NULL value - and then do something with it.
More info about Derived Column Transform
For example transforming String column:
ISNULL([String-Column]) ? "There Was No Value" : [String-Column]

ssis 2005 query parameter date time cast fail

In my SSIS 2005 package, in OLE DB source (SQL Server 2005), I tried to execute a query with one parameter
The query in the OLE DB source
select
...
from..
where
txn_date_time >= ?
As you can see, the query takes one parameter from variable. The variable is type string and value is 20140622
Got this error in the OLE DB source when run the package.
...
Description: "Invalid character value for cast specification".
Also tried this and got the same error.
txn_date_time >= cast(? as datetime)
The query can be run perfectly in SSMS, e.g.
select
...
from..
where
txn_date_time >= '20140622'
It seems to me that SSIS only allow parameter of type date time to be passed to the query because txn_date_time is type date time. Do I have to change the variable to type Date time?
Check the data type of the value returned by your query and the data type of the parameter that you are mapping it to. They should be compatible else you get such cast exceptions.

SQL Task - Set Package variable

I have declared a variable at the package level compdate and am testing data flow to the variable by droping an Execute SQL Task in the Control Flow of the package.
In the task,
SQL Statement:
select ? = (getdate() - 1)
Parameter Mappings:
Variable Name: User::compdate
Direction: Output
Data Type: DATE
Parameter Name: 0
Parameter Size: -1.
Why am i getting error:
[Execute SQL Task] Error: Executing the query "declare #compdate date
set #compdate = (getdate() ..." failed with the following error: "Syntax error or access violation". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I do not see why you need to execute an SQL statement to get the previous day as this can be done in various other ways.
To answer your question though, since you are trying to store the result of the SQL query from your Execute SQL Task you have to change the SQL statement that you have provided.
Your new query:
SELECT (GETDATE() - 1) AS DateVar
Where DateVar will be the single parameter that is returned which you need to map to your variable.
You need to delete your Parameter Mappings as they are not needed. Open up the Result Set tab and Add a new result. Set the Result Name to be DateVar and set the Variable Name to be your variable User::compdate
You then need to set up your Execute SQL Task to return a Single Row result set in the General tab, mapped to your variable. Select Single row for the ResultSet option.
Working with result sets is explained in great details here. Scroll down to the 'Working with a Single-Row Result Set' section, it has a great example which you can follow.
If you want to use without using the result set. try with following steps.
Create the stored procedure in your respective database. Following
code is an example.
CREATE proc GetYesterDay(#yesterday datetime output)
as
Select #yesterday=getdate()-1
Create the ADO.NET connection to run the stored procedure. In which, you can mention the direction of the input and output of the parameters.
Create the execute task and configure it as following screenshot.
Click on Parameter Mapping and configure as following screenshot.
Now SSISCompletedDate variable will be filled with respective data.
Hope this helps!

Storing values in variables using Execute SQL Task Object

I have an execute sql task defined in my package which will execute a stored procedure. When the stored procedure is executed it will return user credential. I have to store the credential in a variables and then use these credentials in a new script task.
The following is what I have tried so far.
STEP 1 - I have created SQL task
On General TAB set following properties:
Resultset: Single row
connectionType: OLE DB
Connection:SourceDestination
SQLSourceType:DirectInput
SQLStatement: Execute dbo.GetLoginInfo1
ByPassPrepare = False
STEP 2 - Parameter Mapping Tab
Variable Name = User::DatabaseUserName
Direction = Output
Data Type = NVarchar
Parameter Name = #UserId
Parameter Size = 50
STEP 3 - Result Set tab
Result Name = 0
Variable Name = User::DatabaseUserName
When I executed the task I got following errors.
[Execute SQL Task] Error: Executing the query "EXECUTE [dbo].[GetUserLoginInfo1]"
failed with the following error: "Value does not fall within the expected range."
Possible failure reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established correctly
When I debug I got to see following information:
Break ON Pre execute:
User::DatabaseUserName = {Hi}
Break ON Post Execute: (error happens but the values gets chanage)
User::DatabaseUserName = {User1}
Sorry just forgot to mention the creation of variables. I have create one variable as listed below.
Name = DatabaseUserName
Scope = Package1
Datatype = String
Value = Hi
The below mentioned is the stored procedure that I have used.
ALTER PROCEDURE [dbo].[GetUserLoginInfo1]
AS
BEGIN
SELECT userid AS userid
FROM login_credentials
WHERE servername= 'server1'
END
I have tried and read lot many sites but I am still facing the problem.This is my second day on it and have to resolve it before tomorrow morning so any help will be really appreciated.
You should completely omit STEP2. (However, your procedure does not have any output column.)
STEP 1, and STEP 3 are all right. (In STEP 3 you could use userid in the Result Name column of the grid, but the ordinal will be absolutely good.)

SSIS Execute SQL Task Out Parameter of -1 not showing up right

This is the goofiest thing I've seen all day.
In SSIS 2005, I have an Execute SQL Task which runs a SQL 2005 stored proc that takes two IN parameters and one OUTPUT parameter. The IN parameters are static and so are hard-coded in the command string. The OUTPUT parameter is pulled into a package variable of type Int32 (although in the Execute SQL Task on the Parameter Mapping page it tells me the data type is LONG).
When I run the SQL Task and the output parameter is returning a value > 0 (like 2), the variable is populated with 2. When I run the SQL task and the output parameter is returning -1, the package variable is populated with some value like 66682316. I can run the proc in SSMS and if the value is pre-populated with -1, it returns -1 to me.
DECLARE #out int
SET #out = -1
EXECUTE MyProc 'param1', 'param2', #out OUTPUT
SELECT #out -- returns -1
Does anyone have any idea why it would be returning this value instead of -1? I'm sure my variable is Int32 and not UInt32.
If you set up your sql command like you did, you should be setting your variable from the result set not from the parameters.
Set Result Set to Single Row, then on the result set tab put 0 (if you are using OLEDB) as your result name and your variable (i.e. User::OutputVariable) as your variable name.
If you want to use parameters, you would set your sql up like this:
EXECUTE MyProc 'param1', 'param2', ? OUTPUT
Then you would go to the parameter mapping tab and set up your parameter as follows:
Variable Name -> User::OutputVariable
Direction -> Output
Data Type -> Long
Parameter Name -> 0
Parameter Size -> -1
NOTE This applies to using OLEDB as the connection type on the general tab. How parameters are named is different depending upon connection type used.
Shoudl you be saying
SELECT ? = #Out