Setting the SQL Command Property using Expressions in SSIS 2005

 

OLE DB Source Adapter

The OLE DB source connection has several Access Modes:

  • Openrowset
  • Openrowset From Variable
  • SQL Command
  • SQL Command From Variable

For now, I am going to focus on the SQL Command options. Using the SQL Command Access Mode requires you to set the SqlCommand property (typing in your SQL query directly), effectively ‘hard coding’ the sql statement into the package. Using the SQL Command From Variable option allows you to set the SqlCommand property using a variable and subsequently by using an expression.

SSIS2005_DataSource_AccessMode_Properties_OLEDBSource

 

Data Reader Source

The Data Reader Source connection has only one Access Mode, and that is SQL Command. Don’t ask me why. At this level you can only set the SqlCommand property by typing in your query, or indeed, copy & paste.

SSIS2005_DataSource_AccessMode_Properties_DataReaderSource

 

Using an Expression to set the SqlCommand Property

“So then,” you ask, “how might I set the SqlCommand property for each of these Data Sources when it seems that I can’t?”. Here’s how. Let’s first concentrate on the OLE DB Source:

  1. Create a new Variable of type string and set your SQL statement
  2. Open up the OLE DB Source and select the ‘SQL Command from Variable’ option
  3. Now, select the the variable created in step 1 as the variable that contains the SQL statement

SSIS2005_DataAccessMode_OLEDBSource_Editor

At this point your OLE DB source is defined by the query that you have set in the Variable. We now need to create the expression that will define the query at runtime. We do this by navigating to the variable’s properties, setting the EvaluateAsExpression property to True and then configuring the expression in the Expression property.

Moving on to the Data Reader Source. With this adapter we only have the option of setting the SqlCommand directly. Or so I thought until recently. If you switch to the Control Flow design surface and select the Data Flow task that contains the Data Reader Source adapter, you will be able to set the SqlCommand using an expression.

  1. Select the Data Flow task to bring up its properties
  2. Click the ellipsis (…) to bring up the Expression Editor
  3. Select the [DataReader Source].[SqlCommand] Property
  4. Set your expression.

SSIS2005_DataSource_AccessMode_Properties_DataFlowTask_Expressions

SSIS2005_DataSource_AccessMode_Properties_DataFlowTask_ExpressionProperties

Using variables & expressions to set your queries enables you to centralise your queries, use configuration files to set database connections and capture the queries you are running in logging operations.  

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>