Setting a variable in an Execute SQL task - ssis

I have an Execute SQL task with an OLE DB source that is running this query:
SELECT OBJECT_ID('AppLogin') AS TableID
In the Result Set tab, I have Result Name 0 mapped to Variable Name User::TableID.
The task executes, but the variable never gets set. It remains 0.

In the Result set Tab, Set the ResultName = TableID
Ensure that you verify this query "SELECT OBJECT_ID('AppLogin') AS TableID" by clicking Build Query Button from General Tab in the Execute Sql Task Editor. It should return the expected value for AppLogin Object.

In the general settings of your Execute SQL task component. Make sure the Result Set is set to "Single Row".

The problem turned out to be the constraints. I had an expression and a constraint going into the Update component.
Didn't realize that the flow wasn't sequential and that the expression and a constraint still need to be OR'ed.

Related

How to increment a user variable in on success and on failure execute sql task in BIDS?

I have a simple package in which I'm executing select statement in Execute SQL Task, I want on success of this task my variable #cnt=0 should increase #cnt by one like #cnt=#cnt+1 and in script task i should be able to show it and on failure it should be like #cnt=#cnt-1.
What should I try in precedence constraint, I tried using this '#CNT==#CNT+1' but it does not increase the value of #cnt on success.
my Expression is
Appreciate any help on this.
You could use the Expression Task to set the value of the variable, with precedence constraint of success, and a separate task to reduce it with constraint of failure.
The expression in the precedence constraint itself is to evaluate whether something should happen, not to set the value for a variable.

SQL Query execution in SSIS

Finding issue in achieving below problem in SSIS.
I have a variable in SSIS #Select which is initialized with a SQL task in SSIS as below.
#Select='Select column1, column2 from tableName', like a dynamic select query, now I want to Execute #Select variable(which should execute select query inside it) to return the full result set in a separate SQL task I have tried it like below but not succeeded.
Declare #Query Varchar(2000)
SET #Query=? // here ? will store the select query in #Select variable
EXEC (#Query) // executing to return result set??
Can anyone help me to achieve this??
If you want to execute a dynamic select query in SSIS then use an Execute SQL Task and edit the task.
Set the ResultSet option to Full Result Set if you are expecting multiple rows and Single Row for only one row. Add your DB connection name to the connection property and ensure that it is in the list of connection managers and configured correctly. You can set the SQLSourceType to Variable and specify the variable you want to use below that. Although using the Direct Input option is just as good where you specify your SQL Statement. Now set the variable you want the ResultSet to write to in the Result Set tab, which is in the left column of the task editor. you can even specify the parameter variable you would like to use in the Parameter Mapping tab.
You can click on Build Query to see if your query works. Hope this helps and let me know if I missed anything :)

SSIS Execute SQL task with parameter

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...

SSIS Execute SQL task based on parameter

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...

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!