Parameter mapping for a table name - ssis

I have a delete statement. Delete from <table name>. I want to make table name dynamic by passing parameter. how can I do this? I have used like delete from ?, but giving other must declared the table variable.

You cannot use the parameter placeholder (?) in the FROM clause.
However you can create a Variable(1) called for example "SQL Query" and define a Expression (2):
The Expression should look like this.
In the Execute SQL Task Editor you can choose Variable under SQLSourceType and under SourceVariable you can choose "SQL Query"

Related

SQL task select top N records where in is a variable from a parameter?

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

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 :)

Setting a variable in an Execute SQL task

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.

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!

How to create a dynamic IN query in SSIS 2008?

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: