SSIS save value as a parameter - sql

I am using SELECT UpdateDate FROM dbo.log command in a execute sql task. I'm fairly new to this so please bear with me. I want to store the value as a variable then pass that into the where clause of a subsequent data flow. My questions are:
What is the correct way to setup the Execute SQL Task. In General I have the OLE DB Connection and direct input with the query above. Result Set is set to Single row and then I am storing this to a variable I have created called User:: UpdateDate. For some reason this doesn't work?
I then want to call this date in a data flow. ie. SELECT * FROM Users WHERE RecordDate > User::UpdateDate. I believe the syntax is different for this.
I would really appreciate some help with this. Many thanks in advance

In your Execute SQL Task Editor, configure the Parameter Mapping as shown below, obviously use your own variable, in this example I'm using PackageStartTime.
Then in your SQL statement, use below:
SELECT * FROM Users WHERE RecordDate > ?
To save value from a SQL Statement, you will need to set the Result Set to single row and configure result set as shown in the example below:

Execute SQL Task with ResultSet
First of all, create a variable of type System.Date example: #[User::UpdateDate].
Add an Execute SQL Task select the OLEDB connection and use the following command as SQL Statement:
SELECT TOP 1 UpdateDate FROM dbo.log
Set the ResultSet property to Single Row and in the ResultSet Tab add a Row with the following values:
ResultName = 0 (which means the first column)
VariableName = #[User::UpdateDate]
Additional Information
SSIS Basics: Using the Execute SQL Task to Generate Result Sets
OLEDB Source with parameterized SQL Command
Inside the Data Flow Task, add an OLEDB Source, select the Access Mode to SQL Command. And write the following command:
SELECT * FROM Users WHERE RecordDate > ?
Click on the Parameters button and map the variable #[User::UpdateDate] as the first parameter.
Additional Information
Map Query Parameters to Variables in a Data Flow Component
Parameterized OLEDB source query

Related

How to pass sql parameter in DAX query in SSIS

Does anyone know how to pass a parameter to DAX query in SSIS?
I know that when using OLEDB Source in SSIS we can use "?" to pass a parameter to the query. To further clarify, the query should look like this 'select region from table where region = ?';
Similarly, how to pass a parameter to a DAX Query within SSIS? I have tried with "#" but it didn't work.
Well, I found the answer at last.
I captured the output of SQL query in a variable using "single Result Set" in Execute SQL task.For example var1 = "tableA"
Then I created another variable for example -DAX_Var which holds the dax query in the form of expression like this "Evaluate Summarize" + #user::var1
Bring a DFT, bring an OLE DB source. Use SQL command from variable and instead of pointing table, I point the variable name. Ie DAX_Var
In this way, I was able to connect my SQL variable and bring it inside the DAX query.
It worked Flawlessly

How to pass ado object to an in clause of a sql command in SSIS

Details (this is specific to SSIS) : There are two database servers : A & B
I have to fetch list of few values from database A (like 1,2,3,4,5 in one column) and pass it in the "IN" clause to a command that will run on database B (like Select something from B.Table where column in (1,2,3,4,5)).
The "Execute SQl task" result set is mapped to an object variable X, which cannot be passed directly as a parameter to the second command.
What are the possible solutions to this?
One possible solution is to use a Script Task to iterate through the Object Variable and build a SQL String to be used in the second Execute SQL task.

How to update parameters in SSIS?

I would like to know if I can update the parameter values (mainly timestamp) automatically using any SQL query. Please don't suggest me to use Environments, as the value is constantly changing and I need them to be updated automatically when I run.
you may follow these steps to config your parameter:
create a variable, e.g. User::starttime
add an 'Execute SQL Task': specify the connection info and the query. e.g. SELECT STARTTIME FROM TABLE. set the ResultSet to 'Single row'. in the ResultSet Tab map the result name and variable name.
after the 'Execute SQL Task' executed, the variable 'User::starttime' will be set to the SQL result.
use the variable User::starttime as your parameter.
Please go throw below link you can use any query output as veritable.
http://dataqueen.unlimitedviz.com/2012/08/how-to-set-and-use-variables-in-ssis-execute-sql-task/

Programmatically edit SQL in Data Flow task (SSIS 2005)

I need to execute a SELECT query with one variable (in the WHERE clause) from an AS/400 source. I have an OLE DB driver, but it does not seem to support using a ? [error is Exception from HRESULT: 0x80040E10 (Microsoft.SqlServer.SQLTaskConnectionsWrap)]
Obviously, this should be do-able by programmatically editing my SQL (I'm guessing from the Script task), but I'm not sure where in the object model I can find the SQL related to my Data Flow task.
If people have other suggestions entirely, I'm open to that as well, I just need to execute a parameterized query where the value replaces the parameter on the client-side before sending the query over. Thanks!
Your best bet here is to create a variable to hold your sql statement.
Then set up the variable as an expression to insert your where clause from the variable you were using for your first attempt.
Your expression would be similar to:
="select col1, col2 from table_name where col3 = " + #[User::WhereClause]
Then you can set the data access mode to SQL Command from variable in your OLEDB source, and set the variable name in OLEDB source to the variable you created.
This way, all of the logic that you already have set up to set the where clause variable will still work as you intended.

How do I pass system variable value to the SQL statement in Execute SQL task?

SSIS 2008. Very simple task. I want to retrieve a System Variable and use it in an SQL INSERT. I want to retrieve the value of System:MachineName and use it in an insert statement.
Using the statement INSERT INTO MYLOG (COL1) SELECT #[System::MachineName] gives the error Error: ..failed to parse. Must declare the scalar variable "#"
Using the statements SELECT #System::MachineName or SELECT ##[System::MachineName] gives the error 'Error Incorrect systax near '::'
I am not trying to pass a parameter to the query. I have searched for a day already but couldn't find how to do this one simple thing!
Here is one way you can do this. The following sample package was created using SSIS 2008 R2 and uses SQL Server 2008 R2 as backend.
Create a sample table in your SQLServer database named dbo.PackageData
Create an SSIS package.
On the SSIS, add an OLE DB connection manager named SQLServer to connect to your database, say to an SQL Server database.
On the Control flow tab, drag and drop an Execute SQL Task
Double-click on the Execute SQL task to bring the Execute SQL Task Editor.
On the General tab of the editor, set the Connection property to your connection manager named SQLServer.
In the property SQLStatement, enter the insert statement INSERT INTO dbo.PackageData (PackageName) VALUES (?)
On the Parameter Mapping tab, click Add button, select the Package variable that you would like to use. Change the data type accordingly. This example is going to insert the PackageName into a table, so the Data Type would be VARCHAR. Set the Parameter Name to 0, which indicates the index value of the parameter. Click OK button.
Execute the package.
You will notice a new record inserted into the table. I retained the package name as Package. That's why the table
Hope that helps.
Per my comment against #ZERO's answer (repeated here as an answer so it isn't overlooked by SSIS newcomers).
The OP's question is pretty much the use case for SSIS property expressions.
To pass SSIS variables into the query string one would concatenate it into an expression set for the SqlStatementSource property:
"INSERT INTO MYLOG (COL1) SELECT " + #[System::MachineName]
This is not to suggest the accepted answer isn't a good pattern, as in general, the parameterised approach is safer (against SQL injection) and faster (on re-use) than direct query string manipulation. But for a system variable (as opposed to a user-entered string) this solution should be safe from SQL injection, and this will be roughly as fast or faster than a parameterised query if re-used (as the machine name isn't changing).
I never use it before but maybe you can check out the use of expression in Execute SQL task for that.
Or just put the whole query into an expression of a variable with evaluateAsExpression set to true. Then use OLE DB to do you insert
Along with #user756519's answer, Depending on your connection string, your variable names and SQLStatementSource Changes