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
Related
I need to execute sql task based on parameter.
Lets say if my #parameter = 1 then execute this sql if #parameter = 2 then execute this sql. I think of a work around but is there anything straight forward such as Len(?) or Len(#parameter1) ..
Bottom line: I need to execute sql query based on what's passed to parameter.
Let me know if that's possible.
If you want an Execute SQL Task to run a different stored procedure based on a variable, then there are a few options:
You could create a stored procedure that takes a parameter. The stored procedure would use IF ELSE code to execute the code as described in a comment by Lamak. This is a less than ideal solution if you want to execute different stored procedures. This could work if you only have a very small number of queries or stored procedures to execute.
You could write a variable that calculates the name of the stored procedure based on an expression. This could work well if you only have a few stored procedures to execute, but it does not scale for a large number of stored procedures. It also is hard to understand from a coding perspective, particularly if the expressions are complex.
You could write a query or stored procedure that generates a separate stored procedure call command. You could run an Execute SQL Task the loads a result set. The result set would map to a variable of Object data type. You could then iterate through the variable in a For Each Container to assign values to variables. Easier to manage than 100 expressions if you have a lot of code to vary.
Based on your comment to me it sounds like you want to try option 2. The following are detailed steps for option 2:
In the Variables window at the package-level scope create a variable called SqlCommand of data type String.
Set the EvaluateAsExpression property for the SqlCommand variable to True.
Click on the expression builder link.
The following is a sample IF THEN ELSE expression using the Conditional operator.
1 == 0 ? "SELECT SomeField = GETDATE();" : "SELECT SomeField = GETDATE() - 2;"
If 1 equals 0, then the first command will be returned. If 1 does not equal 0, then the second command will be returned. In this case, since 1 does not equal 0, the second command is returned. You can change the 1 == 0 section to be the condition you actually want to evaluate.
Add an Execute SQL Task to the control flow.
Open the Execute SQL Task Editor.
Set Connection to your desired database connection manager.
Set SQLSourceType = Variable.
Set SourceVariable to User::SqlCommand.
Close the editor and test the package.
user1810575 has asked this question again in ssis-execute-sql-task-based-on-parameter, see my answer (which is copied here as well).
You cannot use Execute SQL Task to run Transact-SQL statements.
For setting conditional SQL Statement based on what you are trying to achieve.
In Execute SQL Task editor
In general tab, leave the SQLStatement blank.
In parameter mapping tab, add parameter and map User::Parameter variable to Parameter Name 0.
In Expression tab, set the SQLStatementSource to
(DT_NUMERIC, 18, 0) #[User::Parameter]==1 ? ...query 1... : ...query
2...
Can i do something like below, let me know
IF #parameter=1 BEGIN ...query... END IF #parameter=2
Need the correct syntax if it is possible.
It's OLE DB connection.
Not a Stored Proc. just a sql query
DECLARE #param AS INT = ?;
IF #param = 1
BEGIN
SELECT 1 AS Y;
END
ELSE IF #param = 2
BEGIN
SELECT 2 AS Y;
END
There are two question marks in your query and probably you were passing only one variable. I have seen code where developers pass the same value twice (or multiple) times. This is inefficient. A better way is to receive the passed parameters in SSIS variables. Advantages:
1. You need to pass one value only once.
2. More importantly, if you change the order in which the passed parameters are used in the sql, you do not need to change their order on the user-interface of Execute SQL Task Editor//Parameters. This is what Andy Leonard has suggested later in his response.
You can. Assuming you are referring to an Execute SQL Task, the parameters in an Execute SQL Task using an OLE DB connection utilize question marks (?) as parameter placeholders. You map the placeholders to SSIS variables on the Parameter Mapping page of the Execute SQL Task. In the SQLStatement property you would enter:
If (?=1)
begin
... {some T-SQL here} ...
end
If (?=2)
begin
... {some T-SQL here} ...
end
That's one way to accomplish what I think you are asking.
Another way is to create an Execute SQL Task to read the value of #parameter from the database into an SSIS variable. Then you can build two Execute SQL Tasks - one with each option for T-SQL as the SQLStatement property - and use expressions on precedent constraints to determine which Execute SQL Task to execute.
Hope this helps,
:{>
You cannot use Execute SQL Task to run Transact-SQL statements.
For setting conditional SQL Statement based on what you are trying to achieve.
In Execute SQL Task editor
In general tab, leave the SQLStatement blank.
In parameter mapping tab, add parameter and map User::Parameter variable to Parameter Name 0.
In Expression tab, set the SQLStatementSource to
(DT_NUMERIC, 18, 0) #[User::Parameter]==1 ? ...query 1... : ...query 2...
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!
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 = ?
The Error from the execute sql task is
The type of the value being assigned to variable "User::ReturnResult" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
The SSIS package was working fine till date but suddenly it showed an error today .
The script which i wrote in execute sql task is
Declare #ReturnValue int
EXEC #ReturnValue = DATABASE.dbo.StoredProcedure
Select #ReturnValue
The result set is configured to single row and in the resultset
ResultName= 0
VariableName=User::ReturnResult
The return value from the stored proc is either 0 or -1 ( Success or failure )
Since it is a production issue i dont have access to SP or the tables . So can this error occur due to connection issue with the Sybase server or its an issue with the Stored proc
Your error message is telling you that the stored proc is no longer returning just the 0 or -1 you are expecting (could it be returning null?)
Use a tool like sql server profiler to verify the parameters being passed into the stored proc and emulate the same steps to understand why the return type is different.