Thursday, January 15, 2009 1:40 PM rrobbins

SSIS Package - User Variables

A SSIS (SQL Server Integration Services) package is very useful if you want to export data from your database. Recently I wanted to pass some input parameters to my SSIS package and had to struggle to figure out how this can be done. This really isn't very well documented and you can't find detailed instructions on the Internet so I thought I'd blog about it. Get a load of how complicated it turned out to be!

The first step is to add a variable to your SSIS package. In the Business Intelligence Development Studio (Visual Studio 2005), select SSIS > Variables

SSIS-Variables

The first button on the left is for Add Variable. Enter the variable name, scope, data type, and value. The variables will be added to the DTSX file:

   1: <DTS:Variable>
   2:     <DTS:Property DTS:Name="Expression"></DTS:Property>
   3:     <DTS:Property DTS:Name="EvaluateAsExpression">0</DTS:Property>
   4:     <DTS:Property DTS:Name="Namespace">User</DTS:Property>
   5:     <DTS:Property DTS:Name="ReadOnly">0</DTS:Property>
   6:     <DTS:Property DTS:Name="RaiseChangedEvent">0</DTS:Property>
   7:     <DTS:VariableValue DTS:DataType="7">1/1/1950</DTS:VariableValue>
   8:     <DTS:Property DTS:Name="ObjectName">varBirthDate</DTS:Property>
   9:     <DTS:Property DTS:Name="DTSID">{6CE1ED2D-3720-4FDB-9590-FEDC2D78C797}</DTS:Property>
  10:     <DTS:Property DTS:Name="Description"></DTS:Property><DTS:Property DTS:Name="CreationName"></DTS:Property>
  11: </DTS:Variable>

The next step is to map a parameter in your SQL statement to the variable.

  1. Select the Data Flow tab.
  2. Right click on the Source Query
  3. Select Edit
  4. Replace a hard coded value in the SQL statement with a question mark to create a parameter
  5. Click the Parameters... button
  6. Select the variable from the drop down list
  7. Enter the parameter name in the format @Name for a stored procedure parameter or the field name

SSIS-OLE-DB-Source-Editor

SSIS-Set-Query-Parameters

The third step is to add this variable to the package configuration so you can easily change the value in an XML configuration file.

  1. Select SSIS > Package Configurations
  2. Click the Edit button and then the Next button
  3. In the Select Properties to Export dialog box, check the Value property of the variable (which will not appear unless you complete the previous steps to create it first).

SSIS-Package-Configuration-Wizard

The variables will be added to the .dtsConfig file:

   1: <Configuration ConfiguredType="Property" Path="\Package.Variables[User::varBirthDate].Properties[Value]" ValueType="DateTime">
   2:         <ConfiguredValue>1/1/1950</ConfiguredValue>
   3: </Configuration>

The SSIS package can be run by an ASP.NET page or a web service by importing the Microsoft.SqlServer.Dts.Runtime namespace. The input parameter can easily by changed in the package configuration file by loading it as an XML document and editing the appropriate node.

You can use SSIS packages to do very complicated data transformations and exports. Basically it automates everything the SQL Server Import and Export Wizard does. So you can export your database to Access or Excel without repeating that whole process of going through the wizard steps. I've found this particularly useful to set up my own scheduled back up of a remote SQL Server database used by a hosted web application.

Filed under: , , ,

Comments

# SSIS Package - User Variables

Friday, January 16, 2009 11:11 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: SSIS Package - User Variables

Wednesday, February 18, 2009 3:38 PM by pwausdev

FYI to those that are trying to do this with DTEXECUI Set Values option, the Property Path value needs to be the whole string:

For the User Variable = ServerAndPort =>

\Package.Variables[User::ServerAndPort].Properties[Value]

Hope it helps someone...

# re: SSIS Package - User Variables

Thursday, February 19, 2009 8:21 PM by archana

can we edit these variables values in xml file and will it refelect to our ssis package?

# re: SSIS Package - User Variables

Friday, February 27, 2009 4:55 PM by rrobbins

Yes, you can edit the values in the package configuration .dtsConfig file and they will be used when you run the SSIS package.

# re: SSIS Package - User Variables

Friday, March 06, 2009 8:12 AM by renuka popli

its giving me error like " Parameter information can not be derived from sql statement . Set parameter information before preparing  sql command " please let me know why this is happening

# re: SSIS Package - User Variables

Wednesday, April 15, 2009 3:54 PM by HTRAN

How would you go about it if you are querying an oracle database?  The variables don't seem to like to work with Oracle sql.

# re: SSIS Package - User Variables

Wednesday, August 26, 2009 10:33 AM by EronatePw

Is there a way to have a dynamic variable ... for exemple having the current date everytime you just run the package without editing the config file ?

# re: SSIS Package - User Variables

Monday, September 14, 2009 9:51 AM by UsuallyLost

Our variable is called 'currentDT' and always is the current date.  Not sure why which is what I was looking for info about.

# re: SSIS Package - User Variables

Monday, February 08, 2010 3:27 PM by Larry

Is there any possible way to map a variable from your package configuration to your SSIS package??  I know you did it here for an OLE Source, but I don't have one of them.  I have a very simple package that just truncates a work table in SQL, then opens a flat file and fills the SQL table.

Very simple, yet I can't find anything anywhere on how to set the filename in the package config so it's not hard-coded in my package.  Yes, I know how to set the variable in the package config, but I can't find a way to access it in my package!

Thanks!

# re: SSIS Package - User Variables

Tuesday, February 09, 2010 4:33 PM by lARRY

Geez - found the problem!  Let this be a valuable lesson for everyone out there!

The problem turned out to be nothing more than the wrong collation for the fields in the config file on SQL Server.

Try to find that solution in the literature!

# re: SSIS Package - User Variables

Wednesday, September 22, 2010 1:08 AM by nik

How would you go about it if you are querying an oracle database?  The variables don't seem to like to work with Oracle sql.

# re: SSIS Package - User Variables

Tuesday, February 15, 2011 11:35 PM by Nausif

How to use package variables in script component....?

like say i have a package variable named "srctb" of type string that contains the name of table..and i want to use it in script component ...in edit task.....like

INSERT INTO Person.Address2(AddressID, City) " & _

           "VALUES(@addressid, @city)"

instead of "Person.Address2"  i want to pass package variable "srctb"...

how should i proceed....

pls help me

thanx in advance...

# re: SSIS Package - User Variables

Thursday, March 10, 2011 1:14 PM by Piyush

@Nausif

Not sure but maybe u can create a variable which is set to Evalaute as expression as TRUE. THen goto Expression for that variable and write ur whole query

"INSERT INTO" +[@User:srctb] + "(AddressID, City) \" & _

          \"VALUES(@addressid, @city)"

something like this.

ANd then use this variable in ur Tasks.

# re: SSIS Package - User Variables

Friday, March 25, 2011 3:02 PM by generic

Surprisingly! It is like you understand my mind! You seem to know so much about this, just like you wrote the book in it or something. I think that you can do with some pics to drive the content home a bit, but other than that, this is informative blog post. A good read. I’ll definitely revisit again.

# re: SSIS Package - User Variables

Tuesday, May 10, 2011 5:40 AM by Mohit

Can I use variables to change parameters of SSIS package dynamically inorder to execute different web services?

# re: SSIS Package - User Variables

Thursday, July 14, 2011 1:21 PM by Dawn

Thanks for posting *with pictures*.  Am using to make my life easier!

# re: SSIS Package - User Variables

Thursday, July 28, 2011 5:16 PM by Mollie Twidale

I have an SSIS package that reads in values by OLE DB and has the Purchase Order number passed in already. I've added a package variable in the calling to the SSIS package to pass in the username from the web application. My question is: how do I retrieve that value to update that column, which I've called ExportUser, in the SSIS package?

# MSBI # 12 &ndash; SSIS # 5&ndash;Familiar with SSIS Variables from 0 to Expert (Part&ndash;III&ndash; Overview) &laquo; (B)usiness (I)ntelligence Mentalist

Pingback from  MSBI # 12 &ndash; SSIS # 5&ndash;Familiar with SSIS Variables from 0 to Expert (Part&ndash;III&ndash; Overview) &laquo; (B)usiness (I)ntelligence Mentalist

# MSBI # 12 – SSIS # 5–Familiar with SSIS Variables from 0 to Expert (Part–III– Overview)

Tuesday, August 02, 2011 1:05 AM by Vishal Pawar's Blog

Continuing from my last post on variable basics ,you can click here to view MSBI # 10 - SSIS # 3 - Familiar

Leave a Comment

(required) 
(required) 
(optional)
(required)