3

I have an SSIS package where I need to get the date the package last ran from an ADO NET Source then assign it to a variable so what I can use it in a query for another ADO NET Source. I can't find an example on the Googles that actually works. I'm running VS 2012 and connecting to a SQL Server 2012 instance. If there is more information needed let me know.

3
  • Is the last run time a datetime, datetime2 data type or something else? Do you have BIDS Helper installed? Commented Aug 4, 2014 at 20:42
  • @billinkc The LastExecutedOn column is datetime. I'm not familiar with BIDS helper I'm'using VS 2012 for the development. Commented Aug 4, 2014 at 20:52
  • No worries on the bidshelper front. It's a handy tool for all version of SSIS and not tied to a solution. Commented Aug 4, 2014 at 20:55

2 Answers 2

4
  1. Create a variable @User::LastRanDate.
  2. Create an Execute SQL task.
  3. Set the ConnectionType property to ADO.NET.
  4. Set the Connection property to your ADO.NET connection.
  5. Set the SQLStatement property to the statement which will return the date you want. Make sure the first column returned is the date.
  6. Set the ResultSet property to Single row.
  7. On the Result Set tab of the Task editor, hit Add and set the Result Name value to 0 and the Variable Name value to @User::LastRanDate. (ADO.NET result sets are returned as indexed arrays.)

Upon completion of the Task, @User::LastRanDate will now be set to whatever the query returned and you can use it to build up your query for your other ADO.NET source.

Sign up to request clarification or add additional context in comments.

3 Comments

I see the following error [Execute SQL Task] Error: An error occurred while assigning a value to variable "LastExecutedOn": "Result binding by name "LastExecutedOn" is not supported for this connection type. ".
You put the Variable Name in the REsult Name. The Result Name must be 0 (ADO.NET result sets are returned as indexed arrays.)
Thank you!! was going mad looking for something. using your answer as a guide got my own ssis to work.
2

Working with parameterized queries in an ADO.NET Data Source in SSIS is not as easy as an OLE DB one. Basically, you're going to have to write the query with the expression language and pray your source doesn't lend itself to sql injection.

I created a package with 3 variables as shown below

Package

enter image description here

Variables

I have LastRunDate as a DateTime and a QueryAdo as a string. This evaluated as an Expression with the expression being "SELECT RD.* FROM dbo.RunData AS RD WHERE RD.InsertDate > '" + (DT_WSTR, 25) @[User::LastRunDate] + "';"

enter image description here

Execute SQL Task

I create an Execute sql task that uses a query and is set to return a single row. I assign this value into my SSIS Variable.

enter image description here

In my results tab, I assign the zeroeth column to my variable LastRunDate

enter image description here

Data flow

Note there is an expression here. On the ADO.NET source, I originally used SELECT RD.* FROM dbo.RunData AS RD to get my meta data set.

enter image description here

After I was happy with my data flow, I then went to the control flow and substituted my Query variable in as the expression on the ADO.NET Source component (see the referenced questions).

Try it, try it, you will see

I used the following script to build out my demo environment

create table dbo.RussJohnson
(
    LastRunDate datetime NOT NULL
);


create table dbo.RunData
(
    SomeValue int NOT NULL
,   InsertDate datetime NOT NULL
);


insert into dbo.RussJohnson
SELECT '2014-08-01' AS LastRunDate

INSERT INTO
    dbo.RunData
(
    SomeValue
,   InsertDate
)
SELECT
    D.rc AS Somevalue
,   dateadd(d, D.rc, '2014-07-30') AS InsertDate
FROM
(
    SELECT TOP 15 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rc
    FROM sys.all_columns AS SC
) D;

Since I have BIDS Helper installed, I used the following Biml to generate this package as described. For those playing along at home, you will need to edit the third line so that the ADO.NET connection manager is pointing to a valid server and database.

<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <Connections>
        <AdoNetConnection Name="CM_ADO_DB" ConnectionString="Data Source=localhost\dev2014;Integrated Security=SSPI;Connect Timeout=30;Database=tempdb;" Provider="SQL"  />
    </Connections>
    <Packages>
        <Package Name="so_25125838" ConstraintMode="Linear">
            <Variables>
                <Variable DataType="DateTime" Name="LastRunDate" >2014-01-01</Variable>
                <Variable DataType="Int32" Name="RowCountOriginal" >0</Variable>
                <Variable DataType="String" Name="QueryAdo" EvaluateAsExpression="true">"SELECT RD.* FROM dbo.RunData AS RD WHERE RD.InsertDate > '" + (DT_WSTR, 25) @[User::LastRunDate] + "';"</Variable>
            </Variables>
            <Tasks>
                <ExecuteSQL 
                    Name="SQL GetLastRunDate" 
                    ConnectionName="CM_ADO_DB"
                    ResultSet="SingleRow"
                    >
                    <DirectInput>SELECT MAX(RJ.LastRunDate) AS LastRunDate FROM dbo.RussJohnson AS RJ;</DirectInput>
                    <Results>
                        <Result Name="0" VariableName="User.LastRunDate" />
                    </Results>
                </ExecuteSQL>
                <Dataflow Name="DFT POC">
                    <Transformations>
                        <AdoNetSource Name="ADO_SRC Get New Data" ConnectionName="CM_ADO_DB">
                            <DirectInput>SELECT RD.* FROM dbo.RunData AS RD</DirectInput>
                        </AdoNetSource>
                        <RowCount Name="CNT Original rows" VariableName="User.RowCountOriginal" />
                    </Transformations>
                    <Expressions>
                        <Expression ExternalProperty="[ADO_SRC Get New Data].[SqlCommand]">@[User::QueryAdo]</Expression>
                    </Expressions>
                </Dataflow>
            </Tasks>
        </Package>
    </Packages>
</Biml>

3 Comments

Not sure if I'm doing something wrong but I was unable to get this to work. I'm considering abandoning this option.Thank you.
Define "Unable" if you would. There's a lot in my answer so I'd focus on making the most simple of packages manually. Use the screenshots for reference. Add a package with one Variable, LastRunDate. Add an Execute SQL Task to populate it. Does it work? If so, try the data flow without the dynamic/parameter bit. Keep adding complexity until you find exactly what's not working.
Bill, Thank you for challenging me on that. I was too busy trying to get this to fit my solution that I wasn't listening to your example. Once I focused on it that worked. I was then able to then apply to my solution. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.