I have a variable #csv which hold a comma separated value such as:
-a
-a,b
-a,b,c
I need to pass it in a query in my OLE DB source in a data flow to create a query such as:
SELECT COUNT(1) FROM table WHERE col1 IN #csv
So if #csv="a,b" then internally it should resolve into
SELECT COUNT(1) FROM table WHERE col1 IN 'a','b'
How can this be best achieved in SSIS 2008? Can I avoid the script component to create a dynamic query and storing it in a variable?
How can this be best achieved in SSIS
2008? Can I avoid the script component
to create a dynamic query and storing
it in a variable?
The easiest/best way would still be with a script component.
Otherwise you could:
use the csv as data source and select your result
use the and "add column" tool to add the rest of your SQL query around the result
store the result into a variable
Then use a the OLE DB datasource with "query from variable"
You can create a variable to store the query and compose its value using an expression, like:
List of Variables:
Option 1: In case of using OLE DB, select SQL Command from variable and bind the variable #sqlQuery:
Option 2: In case of using ADO.NET, Go to properties of Data Flow Task and expand Expressions and bind the ADO.NET Source > SqlCommand with the variable #sqlQuery + Make sure that ADO.NET Source > Data access mode is a SQL Command:
Option 3: In case of using Execute SQL Task, expand Expressions and bind the SqlStatementSource with the variable #sqlQuery + Make sure that SQL Source Type is a Direct Input:
Related
Ive got another task elsewhere in a package that I build a sql command from a script task, but I now have a simpler sql task that I want to parameterize the number of records I fetch, and drive this parameter via a package variable.
Now:
SELECT TOP 10 col1,col2,col3 from TABLE-A
What I need to use
eg. SELECT TOP ? col1,col2,col3 from TABLE-A
Where ? corresponds to a package variable of type int.
Can I do this with just the SQL task, and not have to derive the statement in something like a script task first?
[ update ]
Solution is what I was suspecting, I just added another script task before the sql task, and generate the statement there. Then change the sql task to use the variable that contains the statement.
Use the variable to build a dynamic sql string in a separate variable, and then execute the dynamic sql string variable.
Parameters can only be used in the WHERE clause.
As Tab mentioned, you will need to create a variable expression to construct the SQL statement. Then, in the Execute SQL Task, you will tell it to use a SQL String (see bullet point 4 in the section "Map query parameters to variables").
https://learn.microsoft.com/en-us/sql/integration-services/control-flow/execute-sql-task?view=sql-server-2017#parameters-in-the-execute-sql-task
declare #intVal as int = 5
select * from
(
select ROW_NUMBER() over(order by Id) as RowNo, * from <tableName>
)a
where a.RowNo <= #intVal
here #intVal will be parameter should be mapped in SQL task
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!
We can write SQL Command from Variable in OLE DB Source Task. How do we get SQL Command from variable in ADO Net Source Task ? Here My Source is MySQL. Because I want to pass a parameter in this SQL Command at WHERE clause. I dont find SQL Command from Variable in ADO.Net Source task. And I cant connect to MySQL in OLE DB Task.
You can use an expression to specify a variable by doing the following:
On the Control Flow, right click on the Data Flow task and select Properties
On the Properties window, click the ellipsis for Expressions
Choose the [ADO NET Source].[SqlCommand] from the Property drop down and click the ellipsis to go to Expression Builder
In Expression Builder you can reference your variables and add in any SQL clauses you need.
Here is a very rough example...