Chapter 33

Making a Package Dynamic with Parameters

SQL 2012 introduced a new parameter paradigm. Parameters enable you to pass in new values for a specific package execution. When your packages are deployed to the new SSIS catalog, an interface is provided to enable you to change the values of parameters prior to running the package. Parameters are very similar to variables, except parameters are easier to change and configure using the new SQL Server Management Studio interface for SSIS.

A parameter, like a variable, is a placeholder that has a name, data type, scope, and value. The value provided in SQL Server Data Tools is called the default or design value. You can replace this value prior to execution. Parameters also have a sensitive property and a required property. When you mark a parameter as sensitive, its value is encrypted in the database and displayed as NULL or *****. When you mark a parameter as required, you must provide the parameter value prior to execution, rather than using the design parameter.

Parameters also have a scope. Parameters can have a package scope or a project scope. A parameter with a project scope can be used in all packages within the project. Package scoped parameters can only be used within the package in which they are defined.

To create a package scoped parameter, choose the Parameters tab in the SSIS designer and create a new parameter by clicking the icon in the top left. Then populate the fields Name, Data Type, Value, Sensitive, Required, and Description. Figure 33-1 shows the completed parameter.

To create a project scoped parameter, open the Solution Explorer window and double-click the Project.params item as in Figure 33-2.

The Project.params window opens, enabling you to create and manage project scoped parameters. See Figure 33-3.

Using the same example as in the previous lesson, you have an Execute SQL Task that is given the duty of deleting rows from the DimEmployee table in the AdventureWorksDW2012 database. The deleted rows should have an EmployeeNationalIDAlternateKey that is equal to a value in a parameter named EmployeeID. Create a package scoped variable name EmployeeID with an integer data type and a value of 2. Write the following query in an Execute SQL Task:

DELETE FROM DimEmployee
      WHERE EmployeeNationalIDAlternateKey = ?

Next, click the Parameters button. On the Parameter Mappings tab, assign the $Package::EmployeeID variable to the value for the question mark placeholder. Select $Package::EmployeeID in the Variable Name field, and enter “0” in the Parameter Name field. The Parameter Name field value will be different for connection types other than OLE DB.

You can use parameters wherever you can use a variable. Why use a parameter instead of a variable? Parameters can be used to store and provide encrypted information for packages, like passwords. Additionally, it is much easier to provide runtime values for parameters. The greatest value of parameters is when you are running a package or using environments for configurations. You learn more about running packages from Management Studio in Lesson 57, and using parameters with environments in Lesson 54.

Try It

In this Try It, you recreate the extract from Lesson 32, except you use a parameter instead of a variable. You then export data from several levels in the organization without opening and changing the package. You do this by changing a project parameter.

The flat file export of employees is based on their level in the organization. After this lesson, you will have an understanding of how to make a package or all packages in a project dynamic using parameters.

This technique will become the foundation you will use for configuration of environments to deploy and move your packages safely from the development environment into the production environment.

You can download the completed Lesson33.dtsx from www.wrox.com.

Lesson Requirements

Create a project parameter named the OrgLevel. The export will contain the employees whose level within the organization is the same value as stored in OrgLevel parameter. Create a flat file named OrganizationLevel.txt that contains all employees with an organization level of 2.

Hints

  • Create a new project parameter that passes a value for the organization level to the OLE DB Source to return only employees with an organization level of 2.
  • Create a flat file that has the following rows:
  • NationalIDNumber
  • LoginID
  • OrganizationLevel
  • JobTitle
  • BirthDate
  • MaritalStatus
  • Gender
  • HireDate

Step-by-Step

1. Create a new package and name it Lesson33, or download the completed Lesson33.dtsx package from www.wrox.com.
2. Open the Project.params window at the top of the Solution Explorer window.
3. To create a new parameter, click the Add Variable icon in the top left of the Project.Params window. Name the parameter OrgLevel and set the value to 2. Figure 33-4 shows the parameter with a Data Type of Int32 and a value of 2.
4. Drag a Data Flow Task onto your Control Flow canvas and name it DFT - Employee Export.
5. Switch to the new Data Flow Task by clicking the Data Flow tab. Add an OLE DB Connection Manager that uses the AdventureWorks2012 database, and then drag an OLE DB Source into your Data Flow.
6. Open the OLE DB Source Editor by double-clicking the OLE DB Source. In the OLE DB Source Editor OLE DB Connection Manager field, choose the connection manager you created in the previous step. Then change the data access mode to SQL Command and enter the following SQL statement:
SELECT NationalIDNumber
,LoginID
,OrganizationLevel
,JobTitle
,BirthDate
,MaritalStatus
,Gender
,HireDate
  FROM HumanResources.Employee
  WHERE OrganizationLevel=?
7. Next, click Parameters item and set Parameter0 to use the project parameter you created earlier: $Project::OrgLevel. Figure 33-5 shows the changes you have just made. Click OK twice to exit the OLE DB Source Editor.
8. Drag a new Flat File Destination from the SSIS Toolbox to the Data Flow window. Connect the OLE DB Source to the Flat File Destination task by dragging the blue line from the source to the destination. Open the Flat File Destination Editor by double-clicking the Flat File Destination
9. Create a new Flat File Connection Manager by choosing the New button in the Flat File Destination Editor. The Flat File Format dialog appears. Delimited is preselected and correct. Click OK. The Flat File Connection Manager dialog will appear. Name the connection manager Organization Level, and set the filename to C:ProjectsSSISPersonalTrainerOrganizationLevel.txt. Also, check the Column names in the first data row option. The path must already exist when you run the package. However, you can create the path within Browse dialog by right-clicking in the parent folder and selecting New.
Click OK to close the Flat File Connection Manager Editor. Select Mappings in the Flat File Destination Editor. Then Click OK to close the editor.
10. The package is now complete. It uses a variable in the WHERE clause of the SQL statement to determine which rows to load into the flat file. Save and close the package. Your package is now reusable.
11. Go to the Solution Explorer window, right-click Lesson33.dtsx, and execute the package. Switch back to design mode and close Lesson33.dtsx. Now open the file C:ProjectsSSISPersonalTrainer.OrganizationLevel.txt. You should see OrganizationLevel value of 2 in the third column. Your results will look like Figure 33-6. Close the text file.
12. Go to the Project.Params window and change the value of the OrgLevel parameter to 1.
13. Go back to the Solution Explorer window, right-click Lesson33.dtsx, and execute the package. Switch back to design mode and close Lesson33.dtsx. Now open the file C:ProjectsSSISPersonalTrainer.OrganizationLevel.txt. You should see an OrganizationLevel of 1 in the third column. Your results will look like Figure 33-7.

Please select Lesson 33 on the DVD, or online at www.wrox.com/go/ssis2012video, to view the video that accompanies this lesson.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset