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...
Related
I am having an issue programmatically setting connection strings.
Here is an overview of my project. I am using 2012 SSIS and have two connections in my connection managers (Source and Destination and both are OLE DB connections). I have multiple packages I want to run. Each of these packages only contain Execute SQL tasks. Each task I have the SQL Statement connection type set to OLE DB, the connection is Destination, the SQL Source Type is a File Connection, and the File Connection is a file located on my C drive that is a script file that I generate in my program.
I have these packages deployed on my server which is a 2012 instance. The source and destination connections are also on my server but on a 2008 R2 instance.
I loop through N number of databases. In each loop I can set the Connection String and IntialCatalog properties by executing a SQL statement:
DECLARE #var sql_variant = N'Data Source=MyServerName\SQL2008R2;Initial Catalog=DatabaseName1;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;'
EXEC [SSISDB].[catalog].[set_object_parameter_value] #object_type=20, #parameter_name=N'CM.Destination.ConnectionString', #object_name=N'MAC', #folder_name=N'MAC', #project_name=N'MAC', #value_type=V, #parameter_value=#var
This does work since I can open up the configure option on my project and see that the connection string and initial catalog values change while I am stepping through the program. (I didn't include the SQL for the initial catalog but it is similar to the connection string)
Now when I execute this through my program, I the following errors:
The connection "{9C2C8088-CE67-4D93-81B8-EC364D6E78D1}" is not found. This error is thrown by Connections collection when the specified connection element is not found.
Is there a way to update the package so the Execute SQL Tasks have an updated connection?
As a side note, if I open the packages in Visual Studio and modify the connections, the packages execute. It is when I try to programmatically change the connections and execute or if I try to execute the packages that are deployed on the server when it errors.
I did see this question and although it is similar, I think it varies enough to have a separate question.
SSIS Connection not found in package. I have found a lot of great info here, just not what I am looking for yet.
Any help/advice is greatly appreciated!
Thanks
Mark
My suggestion would be to move your database looping and connection string setting inside your packages by taking advantage of SSIS's Foreach Loop container.
Create a new string variable in your package. Call it "User::DestinationConnectionString".
Right-click on your Destination connection and choose Properties.
Create an Expression for your connection's ConnectionString property and set to your User:DestinationConnectionString variable.
Put all of your connection strings in a table. Let's call it ConnectionStrings.
Create an Execute SQL task at the beginning of your package. Let's call it "Get Connection Strings."
Set the connection to where your ConnectionStrings table is.
Write a query to retrieve the connection strings.
Set the ResultSet to Full Result Set.
Set your Result Set to a variable of type Object. Let's call it User::ConnectionStrings. (The Result Name should be 0.)
Create a ForEach Loop Container. Name it "For Each Connection String."
Set the Collection Enumerator to Foreach ADO ENumerator.
Set the ADO Object Source variable to User::ConnectionStrings.
In the Variable Mappings tab, map Index 0 to your User::DestinationConnectionString variable.
Place your Execute SQL tasks inside your foreach loop.
Now when the package runs, it will loop through each connection string, assign it to the variable, which the connection uses to get its connection, which the SQL task then runs against.
If you don't know your database connection strings until runtime, you can just write them into the table then and then kick off your package.
Apologies, I'm an SSIS noob and I'm struggling with trying to import a flat file and do an OLE DB command WHILE including a variable in the SqlCommand.
You can see my query and the attempt to include an already defined variable. The error is displayed below:
Must declare the scalar variable "#" I understand what the error is saying, but after hours of searching, I cannot figure out how to use variable in the OLE DB Command.
Thanks in advance for any assistance.
Try putting the ssis variable [user::ClientID] into a derived column and add it to the data flow. You should be able to use a ? and assign that value to your query just like any other value in your data flow.
I have to connect to many Foxpro databases that have a connection string like:
Data Source=\\All Users\\DB0009\db1.dbc;User ID=xxx;Password=yyy;Provider=VFPOLEDB.1;Persist Security Info=True;
where the folder DB0009 can be any integer from 0000 to 0100 and the db1.dbc can be either db1.dbc or db2.dbc for each folder.
For each connection string, I need to build a simple SQL statement that is identical for every database except for a hardcoded year. So the sql is: select *, '2012' from table
I'd like to be able to store both the connection string AND the year for each connection string in a sql table that can be looked up at run time.
I am using SSIS 2008. I'm guessing based on what i've seen that i can use the foreach loop with the enumerator set as the ADO Enumerator (though i wouldn't mind confirmation there), but how can i pull the year off of that same table and put it into the sql query i have inside a data flow task?
Thanks for any help!
Kary
You could do the following steps:
Create the foreach loop to get the connection string and year values into variables.
create a connection manager and, using an expression, set the connection string property to your connection string variable.
Create another variable with an expression that creates your SQL command with the year variable (dynamic SQL command string).
use the execute SQL task to execute this within your foreach.
Here's a link to a blog on mapping the parameter inside the Execute SQL Task that should be helpful. http://www.rafael-salas.com/2007/11/ssis-mapping-parameter-inside-of.html
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:
I'm trying to use an Execute SQL Task in SSIS 2008 to map a store procedure output parameter to a package variable.
The package variable is SSIS type DateTime and the store procedure parameter is SQL type DATETIME.
The SQL Statement is EXEC GetCurrentDate #CurrentDate=? and in the parameter mapping screen, the parameter is mapped to the package variable with direction Output and Data Type DBTIMESTAMP specified.
When I run the package I get the following error:
[Execute SQL Task] Error: Executing
the query "EXEC GetCurrentDate
#CurrentDate=? " failed with the
following error: "The type of the
value being assigned to variable
"User::CurrentDate" differs from the
current variable type. Variables may
not change type during execution.
Variable types are strict, except for
variables of type Object. ". Possible
failure reasons: Problems with the
query, "ResultSet" property not set
correctly, parameters not set
correctly, or connection not
established correctly.
If I run a trace on the query being run I see the type is being assumed as datetime2:
declare #p3 datetime2(7)
set #p3=NULL
exec sp_executesql N'EXEC GetCurrentDate #CurrentDate=#P1 ',N'#P1 datetime2(7) OUTPUT',#p3 output
select #p3
Does anyone know why it is assuming the type is datetime2?
Thanks
Found the answer on a Micorsoft Connect bug report:
We are closing this case as this is expected behaviour and is a result of the new sql datetime type change. You are using a native oledb connection manager for sql task, in the process of COM interop, we use VARIANT to hold the value and the only way to prevent data loss is to store the value as BSTR variant. If you change User::dateParam to String type it will work, or you can switch to use managed connection manager to bypass the COM interop.
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=307835
Try specifying the inout/output parameters as DATE rather than DBTIMESTAMP in the SSIS task.
This certainly works in SSIS 2005 packages I've worked on.
It's also worth taking a look at this link, which covers this as well as a couple of other issues with SSIS and dates.