Sometimes when writing an SSIS package, we need to update the value of a package variable based on a database table. For example, we may have a variable called ImportFolder. This package variable contains the path to the folder which contains the files that we need to import. The value of ImportFolder variable is stored in the database. We have to query that database (select ImportFolder from table1 where col1 = …) and then set the value of the ImportFolder package variable.
In some cases the value that we need to update the package with is not stored in the database, but it is stored in a file. We need to open this file, read it and set the value of the package variable. In this article I will give an example on how to do the database one, and on the next article I’ll do the file one.
Execute SQL Task
We use an Execute SQL Task, then set the Result Set to a variable. The database doesn’t have to be SQL Server of course, it can be anything. Here’s how:
Pull Execute SQL task from the Toolbox to the Control Flow canvas:
Double click it to edit and type the query on the SQL Statement and set Result Set to Single Row.
Here’s an example of a SQL query to get the Import Folder from a database table:
After you set the SQL Statement and Result Set, click on the Result Set pane on the left handside and set the Result Name and Variable Name. Result Name is the column name on the query output and variable name is the name of the package variable (yes you need to create it first).
So click OK to come out of Execute SQL Task editor, create the variable as below.
Create the variable by clicking the first icon. You need to set the Data Type as String. To set the scope to Package, click on any point on the Control Flow canvas before creating the variable.
If you wonder “Where is this Variable window?” scroll down to the bottom of the screen, click Variables tab on the left, next to the Toolbar tab:
Now back to Result Set tab again (double click Execute SQL Task, click on Result Set tab on the left) and set the Result Name and Variable Name:
Run it. Before running it you may want to set a Breakpoint so you can see the value of the variable. Right click on the Execute SQL Task and choose Edit Breakpoints. Select the OnPostExecute event (this is after the task is run).
Then run the package. It will stop after it runs the Execute SQL task. To see the value of the ImportPath variables, type “ImportPath” in the Watch window:
Or expand the Locals window and find User::ImportPath:
If you wonder “Where are these Watch and Locals windows”, they are at the bottom of screen. If you can’t see them try: Windows menu, Reset Windows Layout.
Vincent Rainardi, 6/6/2011
PS. On the next article I’ll write about Updating a Variable from a File.
[…] setting the value of a variable from database Link […]
Pingback by Coming SSIS Articles « Data Warehousing and Business Intelligence — 6 June 2011 @ 9:20 pm |
[…] I wrote about setting a variable based on the value in a database table (link). Sometimes the source value is not stored in a database table but in a file. We need to read that […]
Pingback by Update a Variable based on a File – Script Component « Data Warehousing and Business Intelligence — 7 June 2011 @ 7:16 pm |
[…] https://dwbi1.wordpress.com/2011/06/06/ssis-updating-a-variable-based-on-database/ […]
Pingback by SSIS: Update variable – Viktor's storage — 20 March 2018 @ 8:11 am |