CHAPTER 18

image

Configuration

SQL Server 2012 introduces Parameters – a new configuration model for SSIS. This new model is meant to simplify the configuration process, and make it easier for users to identify where values are coming from at runtime. While 2005/2008 style Package Configurations are still supported in SQL Server 2012, the two configuration models are not meant to be mixed. In fact, the menu option to use them will only appear when using the File Deployment Model, and on packages that have been upgraded from previous versions. New packages created in 2012 will use the new Parameter model by default.

This chapter describes the new Parameter model, and how it can be used to configure package properties at runtime. We’ll look at how Parameters are exposed in the SSIS Catalog, and how you can set Parameter values as part of your build process using Visual Studio Configurations. Finally, we’ll look at design patterns that can used to augment the functionality provided by the built-in Parameter model, providing dynamic runtime configuration.

Parameters

SSIS Parameters allow packages to define an explicit contract, much like function parameters do in programming languages like C#. Unlike package configurations, parameters are exposed to the callers, like SQL Agent, or the Execute Package Task, so users are able to see exactly what a package needs to run. Parameters are essentially read-only package variables in a special namespace. They follow the same type system as package variables, and will appear in all of the same UIs that variables do (for example, for setting property expressions). You’ll make use of parameter values through Expressions, or by reading them in a Script Task. Parameter values are set before package execution begins, and their value cannot be changed while the package is running.

Parameters can be defined at the package level, and the project level. Package level parameters are visible only to tasks and components within that package – much like package variables. Package parameters are defined in the $Package namespace. Parameters defined at the Project level are global – all packages within the project are able to make use of them. Project parameters are defined in the $Project namespace.

Figure 18-1 shows the new Parameters tab in BIDS, which displays parameters defined at the package level. In addition to the standard properties you’d find on a package variable (such as Name, Type, and Value), Parameters expose three new properties: Description, Sensitive, and Required.

9781430237716_Fig18-01.jpg

Figure 18-1. Package level parameters are created and displayed on their own tab in BIDS

The Description field provides an easy way for the SSIS package developer to document the arguments for their packages. It’s recommended that you provide descriptions for your parameters, especially in cases where the person running or configuring the packages is not the same person that developed them.

If a parameter is marked as Sensitive, its value will be stored in an encrypted format within the package. Its value will also be masked when it is displayed in the UI, and will not be displayed in execution logs. Sensitive parameters can only be used in Expressions for properties that are marked as Sensitive (such as the Password property of a Connection Manager).

Parameters that are marked as Required must have their values specified at runtime. All parameters (and variables) need values set at design time for validation purposes. Required parameters will not use this design time value when the package runs – a new value must be specified by the caller (i.e., SQL Agent, or the parent Execute Package Task). If a parameter’s Required property is set to False, the parameter becomes optional – its design time value will be used if no other value is supplied. Parameters that have no logical default value (such as a BatchID, or path to an input file) should be marked as Required.

Project level parameters can be found by accessing the new node in the Solution Explorer (as shown in Figure 18-2). Project parameters appear in their own node because they are stored in a separate file (Project.params) within the solution directory. Double clicking this node brings up the same parameter designer used for package parameters, with all of the same properties and options.

9781430237716_Fig18-02.jpg

Figure 18-2. Project level parameters can be found in the Project.params node in Solution Explorer

Configuring Your Package Using Parameters

Parameter values are used in your package via SSIS Expressions. Expressions can be set on most Task properties, variables, and certain component properties in a Data Flow Task. To set an expression on a Task, open the Property Expressions Editor dialog (shown in Figure 18-3) by clicking on the Expressions property in a Task’s Properties window. Expressions can be set on variables directly from the Variables window (as shown in Figure 18-4). In SQL Server 2012, adding an expression to a variable automatically sets its EvaluateAsExpression property to True – in previous versions of the product, you had to perform this step yourself. You can disable expression evaluation for a variable by setting this property back to False.

9781430237716_Fig18-03.jpg

Figure 18-3. The Property Expressions Editor shows all properties that have expressions set on them

9781430237716_Fig18-04.jpg

Figure 18-4. Expressions can be set directly from the Variables window in SQL Server 2012. Variables that have an expression set on them appear with a special icon.

Figure 18-4 also shows a new feature in SQL Server 2012 – Expression Adorners. The icons for Tasks, Connection Managers, and Variables will change if any of the object’s properties is set via expression, providing a visual way for a developer to identify which parts of a package are being set dynamically.

Setting expressions for Data Flow components is less straightforward than setting them on Tasks. The main differences are that the expressions are set on the Data Flow Task itself, and not all component properties are expressionable. Figure 18-5 shows how expressionable properties on a Lookup Transform “bubble up” and appear as properties on the Data Flow Task.

9781430237716_Fig18-05.jpg

Figure 18-5. Expressionable Data Flow Component properties will show up as properties on the Data Flow Task

Package and Project level parameters will appear in all of the UIs that display the list of available Variables. On the Expression Builder dialog (Figure 18-6), all parameters appear under the “Variables and Parameters” folder.

9781430237716_Fig18-06.jpg

Figure 18-6. Parameters appear alongside variables in the Expression Builder dialog

Certain Tasks and Data Flow components are able to make use of variable and parameter values without the use of expressions. For example, the OLE DB Source provides a “SQL command from variable” data access mode that allows you to set the source query from a variable. Parameters can be used instead of variables for all such properties.

Using the Parametrize Dialog

SSIS provides a Parameterize UI (shown in Figure 18-7), which acts as a shortcut for making use of parameters in your packages. From this UI, you can create a new parameter, or make use of one that already exists. When you click OK, SSIS will automatically add an expression to the selected property. To launch the Parameterize UI, right click on and select “Parameterize” from the context menu.

9781430237716_Fig18-07.jpg

Figure 18-7. The Parameterize UI is a shortcut for making use of parameters in your package

Creating Visual Studio Configurations

Another new SSIS feature in SQL Server 2012 is the use of Visual Studio Configurations, which allow you to create multiple sets of parameter values within BIDS. Switching between configurations allows you to easily change parameter values during development, and also allows you to build multiple versions of your project deployment file with different default parameter values. Visual Studio Configurations are a way for developers to maintain their own settings in multi-developer or team environments.

When you first create a project within BIDS, you will have a default configuration called Development. You can create additional from the Configuration Manager dialog (shown in Figure 18-8). You can launch the Configuration Manager dialog from the Solution Configurations combo box on the Standard toolbar, or by right clicking on the project node in the Solution Explorer, selecting Properties, and clicking the Configuration Managers button. To create a new configuration, select the < New ... > option from the Active solution configuration drop down.

9781430237716_Fig18-08.jpg

Figure 18-8. Visual Studio Configurations can be managed from the Configuration Manager dialog

To add a parameter to a configuration, click the Add Parameters to Configurations button on the package Parameters tab. Figure 18-9 shows the Manage Parameter Values dialog that will be displayed when adding package parameters to configurations. Clicking the Add button allows you to select a parameter – once a parameter is added, it will appear in all configurations in the solution. The Remove button will remove the selected parameter from configurations (which means it will always have the same default value at design time). The Sync button will apply the same value to all configurations – use this button as a shortcut when you’re sure that the parameter’s default value should change across all configurations. Currently only package parameters and project parameters can be added to Visual Studio Configurations, but they are configured from separate dialogs. To manage project level parameters, click the Add Parameters to Configurations button from the Project Parameters designer (Project.params). To manage a Connection Manager’s settings with Visual Studio Configurations, you will first need to parameterize the Connection Manager. Shared Connection Managers cannot be configured using Visual Studio Configurations.

9781430237716_Fig18-09.jpg

Figure 18-9. The Manage Parameter Values dialog displays all parameter currently set via configurations

image Note  When a parameter is controlled by Visual Studio Configurations, its value is saved out to the Visual Studio project file (.dtproj). Be sure to save the project file after making updates to your configurations to make sure that the changes are not lost.

Specifying Entry Point Packages

SQL Server 2012 introduces another new concept for SSIS – Entry-point Packages. This feature allows the package developer to indicate that special attention should be paid to certain packages. This is very useful in projects that contain a small number of “master” packages that run a number of child packages. Note that packages that are not marked as entry point packages can still be run – the setting is meant to be a hint for the person configuring parameter values in the SSIS Catalog. Most SSIS UIs in SSMS allow you to quickly filter out parameters on non-entry point packages, allowing you to view only the parameters they need to set.

Packages are marked as entry points by default. To remove this setting, right click on the package name in the Solution Explorer, and unselect the Entry-point Package option.

Connection Managers

Most Connection Managers will require some form of configuration, and in SQL Server 2012 all Connection Manager properties are configurable when packages are run through the SSIS Catalog. As these properties are already exposed, in most cases you will not need to expose additional parameters for your Connection Managers. However, you may encounter some scenarios where parameterized Connection Managers will be beneficial. Note that any Connection Manager property that is set via expression will not be exposed through the SSIS Catalog, which prevents a DBA from accidentally overriding property values are set at runtime.

image Note  In previous versions of SQL Server Integration Services, it was very common for child packages to configure Connection Managers with variable values from the parent package. You may wish to keep this pattern in SQL Server 2012 if the connection string is determined at runtime; however, in many cases you’ll want to use Shared Connection Managers instead.

Parameters can be set on Connection Managers using property expressions. The most common property to set via expression is the ConnectionString, as many Connection Managers derive their properties by parsing the ConnectionString value. When configuring Connection Managers, be sure to set expressions on either the ConnectionString, or individual properties – the order that expressions are resolved cannot be guaranteed, and certain properties may be overwritten when the ConnectionString is applied.

To parameterize a Shared Connection Manager, open up one of the packages in the project and right click on the Shared Connection Manager’s name in the Connection Managers area of the design surface. Note that since Shared Connection Managers are declared at the project level, you can only use project level parameters or static strings in any property expressions on Shared Connection Managers. The expression dialog will not give you the option to use package parameters or variables.

Parameter Configuration on the Server

Parameters were designed to make it easier for the person scheduling and running SSIS packages. In many environments, this is typically a DBA or IT operations person – not the person who originally developed the package. By including descriptions with the parameters, an ETL developer can create self-documenting packages, making it very easy for whoever is configuring the package to see exactly what it needs to run.

This section describes how packages are configured through the SSIS Catalog, and how parameters are surfaced through SSMS. It covers how to set default parameter values after a project is deployed, the various package execution options, and how the built in reporting functionality in SQL Server 2012 make it easier to determine the exact configuration values set when the package was run.

Default Configuration

Default values for all parameters and connection managers are saved within the SSIS project deployment file (.ispac) when the file is built. These become the default values for the project once it is deployed to the SSIS Catalog. To change the default configuration, right click on the project name (or individual package names) and select Configure ... within the SSMS Object Explorer (as shown in Figure 18-10).

9781430237716_Fig18-10.jpg

Figure 18-10. The default configuration for a project can be changed through SSMS after the project is deployed

Figure 18-11 shows the parameter configuration dialog in SSMS. Through this dialog, you can set default values for all parameters and connection manager properties for packages within this project. The Scope dropdown allows you to filter your view of the parameters and connection managers. The default view will display Entry-point packages only, but you can also view parameters for individual packages, and for the entire project. To change the value for parameter or connection manager property, click the ellipse button at the end of the row. You will have three options when changing a value: use the project default; set a literal value; or use a server environment variable. For more information about environments, see the next section.

9781430237716_Fig18-11.jpg

Figure 18-11. Parameter configuration dialog

Server Environments

Server Environments contain a set of variables – essentially name value pairs – that you can map to parameters and connection manager properties within your project. When you run a package through the SSIS Catalog, you can select an Environment to run it in. When a value is mapped to a server environment variable, its value will be determined by the environment is it currently running in.

Before you can map a value to server environment variable, you must associate the environment with the project. Figure 18-12 shows the References tab of the project configuration dialog which allows you to associate a project with one or more environments.

9781430237716_Fig18-12.jpg

Figure 18-12. The References tab of the Configure dialog lets you associate a project with environments

Like Projects, Environments are contained within a Folder in the SSIS Catalog. A project may reference an environment in any folder in the Catalog – references are not limited to the current folder only. If you plan to use Environments throughout your projects, you might consider creating a separate folder as an area to store all of the common environments.

Environments support row-level security. Like Projects and Folders, you can configure which users or roles have access to individual Environments. Users will not be able to see Environments they do not have access to.

Once a project has been associated with one or more server environments, you are able to map parameter and connection manager values to variables contained within those environments.

image Note  Environments can contain any number of Server Variables, and two environments might not contain variables with the same name. If a parameter or connection manager value is mapped to a Server Variable, only Environments which contain a variable with that name (and matching data type!) will be available when selecting the Environment to run the package in.

Default Parameter Values Using T-SQL

Default parameter values and connection manager properties can be set through the SSIS Catalog’s T-SQL API. This allows a DBA to automate the setting of parameter values after a deployment, or after a project is moved to a new SSIS Catalog. An easy way to create a script is to make the changes through the parameter configuration UI, and then clicking the Script button. Listing 18-1 shows the T-SQL used to set default values for a two items; a package parameter (MaxCount) is to set 100, and a connection manager property (CM.SourceFile.ConnectionString) is set to 'C:DemosDataRaggedRight.txt'.

Listing 18-1.  Setting parameter values using T-SQL

DECLARE @var sql_variant = N'C:DemosDataRaggedRight.txt'
EXEC [SSISDB].[catalog].[set_object_parameter_value]
        @object_type = 20,
        @parameter_name = N'CM.SourceFile.ConnectionString',
        @object_name = N'ExecutionDemo',
        @folder_name = N'ETL',
        @project_name = N'ExecutionDemo',
        @value_type = V,
        @parameter_value = @var
GO

DECLARE @var bigint = 100
EXEC [SSISDB].[catalog].[set_object_parameter_value]
        @object_type = 30,
        @parameter_name = N'MaxCount',
        @object_name = N'LongRunning.dtsx',
        @folder_name = N'ETL',
        @project_name = N'ExecutionDemo',
        @value_type = V,
        @parameter_value = @var
GO

image Note  “For more information, see the set_object_parameter_value stored procedure entry in Books Online: http://msdn.microsoft.com/en-us/library/ff878162(sql.110).aspx

Package Execution through the SSIS Catalog

Default values for parameters and connection manager properties can be overridden when a package is executed. The Execute Package UI in SSMS (shown in Figure 18-13) allows you to specify the values to use for that specific execution of the package. Project and package level parameters are displayed on the Parameters tab, and shared connection managers and package level connection managers are shown in the Connection Managers tab. The advanced tab allows you to override property values that were not exposed as parameters. This feature – called Property Overrides – allows a DBA to make a quick configuration change to a value within a package without having to redeploy the entire project. The functionality is similar to using the /Set command line option with DTEXEC.

9781430237716_Fig18-13.jpg

Figure 18-13. Interactive package execution through SSMS

The Execute Package UI also has a Script button, which allows you to script out the creation of a package execution to T-SQL. Listing 18-2 provides an example of creating a new package execution and overriding a number of settings. This procedure involves a number of steps:

  1. Create a new Execution instance using [catalog].[create_execution]
  2. Override parameter or connection manager values using [catalog].[set_execution_parameter_value]
  3. Set property overrides using [catalog].[set_execution_property_override_value]
  4. Start the package execution using [catalog].[start_execution]

Listing 18-2.  Running a Package Using T-SQL

-- Create the package execution
DECLARE @exec_id bigint
EXEC [SSISDB].[catalog].[create_execution]
        @execution_id = @exec_id OUTPUT,
        @package_name = N'LoadCustomers.dtsx',
        @folder_name = N'ETL',
        @project_name = N'ExecutionDemo',
        @use32bitruntime = 0
-- Set a new value for the AlwaysCheckForRowDelimiters property of the
-- SourceFile connection manager
EXEC [SSISDB].[catalog].[set_execution_parameter_value]
        @execution_id = @exec_id,
        @object_type = 20,
        @parameter_name = N'CM.SourceFile.AlwaysCheckForRowDelimiters',
        @parameter_value = 0
-- Set the logging level for this execution
EXEC [SSISDB].[catalog].[set_execution_parameter_value]
        @execution_id = @exec_id,
        @object_type = 50,
        @parameter_name = N'LOGGING_LEVEL',
        @parameter_value = 1
-- Create a property override for the MaxConcurrentExecutables property
EXEC [SSISDB].[catalog].[set_execution_property_override_value]
        @execution_id = @exec_id,
        @property_path = N'Package.Properties[MaxConcurrentExecutables]',
        @property_value = N'1',
        @sensitive = 0
-- Start the package execution
EXEC [SSISDB].[catalog].[start_execution] @exec_id
-- Return the execution ID
SELECT @exec_id

GO

The Integration Services job steps in SQL Agent has been enhanced in SQL Server 2012 to support running packages stored in an SSIS Catalog. The user interface is the same as when you run a package interactively through SSMS, and provides the same configuration options. Alternatively, you can run SSIS packages using the T-SQL job step. However, as this step does not support the use of Proxy Accounts, you will be limited to running the packages as the SQL Server Agent service account.

Parameters with DTEXEC

The command prompt utility to run SSIS packages (DTEXEC) has been updated to support projects and parameters. DTEXEC is able to run packages stored within an SSIS project file (.ispac), as well as start a server-based execution of a package stored within an SSIS Catalog (local or remote). Both modes use different command line switches to set parameter values, and are described in separate sections below.

image Note  When working with individual SSIS package files (.dtsx), DTEXEC behaves the same as it did in previous versions of SQL Server. For more information on the various command line options for DTEXEC, see its entry in Books Online: http://msdn.microsoft.com/en-us/library/ms162810.aspx

Projects on the File System

While the new Project Deployment Model is primarily meant for use with the SSIS Catalog, it is possible to run packages within a project file using DTEXEC. Packages run this way are executed locally by the DTEXEC process. Individual parameter values can be set using the /Set option, and /ConfigFile can be used to set a number of parameter values from a 2005/2008 style XML configuration file. Table 18-1 provides a summary of the options related to running packages from projects stored on the file system.

Table 18-1. DTEXEC Command Line Options for Using Project Files (.ispac)

Parameter Description
Proj[ect] = path_to_project This option provides the path to the SSIS project file (.ispac).
Example: /Proj c:demoproject.ispac
Pack[age] = package_name The name of the package within the project file you want to run. The value should include the .dtsx extension.
Example: /Pack MyPackage.dtsx
Set = parameter_name;value This option allows you to set a value for a parameter within the project. The syntax is similar to what you’d use to override package variable values on the command line. Use the $Project namespace to set values for parameters defined at the Project scope, and $Package for parameters defined at the Package scope.
Example: /Set = Package.Variables[$Project::IntParameter];1
Conf[igFile] = path_to_file This option allows you to set multiple parameter values from an XML configuration file. The syntax for each parameter value is similar to what is used for the /Set option.
Example: /Conf parameters.xml

Listing 18-3 provides an example of running a package (MyPackage.dtsx) contained within a project file (project.ispac). It sets the values for two parameters – BatchNumber, an integer parameter defined at the Project level, and HostName, a string parameter defined at the Package level.

Listing 18-3.  Running Packages Within a Project File Using DTEXEC

dtexec.exe /Project c:demoproject.ispac /Package MyPackage.dtsx /Set
Package.Variables[$Project::BatchNumber];432 /Set
Package.Variables[$Package::HostName];localhost

image Note  Although the syntax for setting parameter values is similar to setting values for variables and other package properties, there is one key difference. To set parameter values, you should not include the name of the property (i.e., ".Value") – you only specify the name of the parameter itself.

Projects in the SSIS Catalog

DTEXEC has been extended in SQL Server 2012 to support running packages contained within an SSIS Catalog. Unlike other execution modes, when running a package from a Catalog, the execution takes place on the SSIS Catalog’s server, and not by the DTEXEC process. In this mode, you will use the /ISServer command line option to specify the path to the package you want to run, the /Parameter option to set parameter values, and the /EnvReference option if you wish to run your package in a specific server environment. Table 18-2 contains a full list of command line options for SSIS Catalog based execution with DTEXEC.

Table 18-2. DTEXEC Command Line Options for the SSIS Catalog

Parameter Description
Ser[ver] = server_instance The name of the SQL instance containing the SSIS Catalog. If this option is not specified, the default instance on the localhost is assumed.
Example: /Ser ETLSERVER1
IS[Server] = path_to_package The path of the package in the SSIS Catalog. This will contain the name of the catalog (SSISDB), the folder name, the project name, and the name of the package you want to run. This option cannot be used with the /DTS, /SQL, or /FILE options.
Example: /IS SSISDBMyFolderETLProjectMyPackage.dtsx
Par[ameter] = name[(type)];value Set a value for the given parameter. Include the namespace of the parameter along with the name to distinguish parameter scope ($Project for project level parameters, $Package for package level parameters, $CM for connection manager properties, and $ServerOption for server specific options). If the namespace is not included, the parameter is assumed to be at the package scope.
Example: /Par $Project::BatchNumber;432
Env[Reference] = environment_id This option allows you to specify a server environment to use when running a package. Any parameter values that have been bound to server environment variables will be resolved automatically. To get the ID for an environment, query for its name in the [catalog].[environments] view in SSISDB.
Example: /Env 20

Listing 18-4 provides an example of running a package (MyPackage.dtsx) contained within a project (ETLProject) in a folder (MyFolder) on a remote SSIS Catalog server (ETLServer). It sets the values for two parameters – BatchNumber, an integer parameter defined at the Project level, and HostName, a string parameter defined at the Package level. It also sets the SYNCHRONIZED server option to True, which tells DTEXEC to run in a synchronous mode – more details on synchronous vs. asynchronous execution can be found below.

Listing 18-4.  Running Packages Within an SSIS Catalog Using DTEXEC

C: > dtexec.exe /Ser ETLServer /IS SSISDBMyFolderETLProjectMyPackage.dtsx /Par
$Project::BatchNumber;432 /Par $Package::HostName;localhost /Par
"$ServerOption::SYNCHRONIZED(Boolean)";True

Microsoft (R) SQL Server Execute Package Utility
Version 11.0.2100.60 for 64-bit
Copyright (C) Microsoft Corporation. All rights reserved.

Started: 4:46:44 PM
Execution ID: 4.
To view the details for the execution, right-click on the Integration Services Catalog,
and open the [All Executions] report

Started: 4:46:44 PM
Finished: 4:49:45 PM
Elapsed: 3 seconds

image Note  You must use Windows Authentication to connect to your SQL Server instance when running packages contained in an SSIS Catalog. The /User and /Password command line options cannot be used with the /ISServer option. If you need to impersonate another user account, you can use the RunAs DOS command with DTEXEC.

When you run an SSIS Catalog package with DTEXEC, it will run in an asynchronous mode by default. This means that the process will return immediately, and will not tell you whether the package actually ran successfully. To get synchronous execution behavior (e.g., the same that you would get when running packages from the file system, or MSDB), you need to include the /Par "$ServerOption::SYNCHRONIZED(Boolean)";True command line switch. When synchronous execution is used, the DTEXEC process will not return until the package has finished running.

Another difference between SSIS Catalog and other forms of DTEXEC execution is that the events that occur while the package is running are not displayed on the command line. Listing 18-4 shows a sample output from running a package in the SSIS Catalog – as you can see, there is only a single message telling you the server execution ID, and pointing you to the catalog reports.

Dynamic Configurations

Parameters on an entry point package allow a user to specify values, but they require that the values be known before the package starts running. There may be times where you’ll need to determine configurations at runtime, or dynamically pull in values from other sources (such as an external file, or database table). The following sections provide design patterns that can be used to augment the capabilities provided by the Parameter model.

Configuring from a Database Table

The SSIS Catalog provides a central location for package configuration values, but your environment may already have alternative locations that store metadata that your packages need at runtime. This pattern shows how to retrieve values from a database table using an Execute SQL Task and configure properties within the package using property expressions. For this example, you’ll be reading a directory and file name from a database, storing the values in variables, and then using them to dynamically set the ConnectionString for a Flat File Connection Manager.

Creating the Database Table

Listing 18-5 shows the SQL for the table that you will be reading your configuration values from. Each row in the table is a new flat file that you will want to process with this package. The two main columns you are interested in are directory and name – the id column is a surrogate key to uniquely identify each row in the table, and the processed column lets us easily filter out files that have already been processed. Sample values are shown in Table 18-3.

Listing 18-5.  SQL definition of the table our package will read its configuration values from

CREATE TABLE [dbo].[PackageConfiguration]
(
        [id] int IDENTITY(1,1) NOT NULL,
        [directory] nvarchar(255) NOT NULL,
        [name] nvarchar(255) NOT NULL,
        [processed] bit NOT NULL
)

Table 18-3. Sample Rows from the PackageConfiguration Table

image

Retrieving Configuration Values with an Execute SQL Task

You will retrieve the list of files you need to process from the PackageConfiguration table you created using an Execute SQL Task. You will store the result set in a package variable, and then loop through each row with a Foreach Loop Container. You will use the processed field to mark the files that have already been processed – you will set the processed value to True once you have successfully loaded the file.

image Note  This example assumes that all of the flat files listed in the PackageConfiguration table have the same schema. It does not cover the logic needed to actually load the flat file into the database – it is meant to illustrate the pattern that you’d use as a template for processing a number of items in a loop.

Setting up the package takes the following steps:

  1. Add four package variables

    a.   FileID (Int32) – the row id for the file you are currently processing

    b.   Directory (String) – the directory containing the flat file you need to process

    c.   FileName (String) – the name of the file you are processing

    d.   FilesToProcess (Object) – the result set of the Execute SQL Task

  2. Add an Execute SQL Task to your package – name it “Retrieve File List”
  3. Double click the Task to open its editor
  4. Ensure the ConnectionType is OLE DB
  5. Click on the Connection drop down and select < New connection ... >
  6. Click New, and configure the connection manager to point to the database containing the PackageConfiguration table
  7. Select all of the files that have not been processed from the PackageConfiguration table (as shown in Listing 18-6)

    Listing 18-6.  Query to Pull Out All Entries in the Configuration Table that Have Not Been Processed Yet

    SELECT * FROM [PackageConfiguration] WHERE [processed] = 0
  8. Set the ResultSet value to Full Result Set. This means that the Execute SQL Task will retrieve the values as an ADO Recordset that can be processed by the Foreach Loop. Note that you could also use an ADO.NET Connection Manager here, which would cause the results to be returned as an ADO.NET DataTable.
  9. Click on the Result Set tab
  10. Click Add, and use these mappings

    a.   Result Name – 0

    b.   ariable Name – User::FilesToProcess

  11. Click OK to save the changes to the Execute SQL Task
  12. Add a Foreach Loop container to your package
  13. Connect the Execute SQL Task to the Foreach Loop Container
  14. Add a Data Flow task inside of the Foreach Loop Container
  15. Add a new Execute SQL task inside of the Foreach Loop Container
  16. Connect the Data Flow task to the Execute SQL Task
  17. Double click the Execute SQL Task to open its editor
  18. Set the Connection to the same connection manager you created in step 5
  19. Listing 18-7 shows the SQLStatement to mark a row in the table as processed. Note that the statement contains a parameter marker (the question mark). You will map a variable value to this parameter in the next step.

    Listing 18-7.  SQL statement to mark the file as processed

    UPDATE [PackageConfiguration] SET [processed] = 1 WHERE id = ?
  20. Click the Parameter Mapping tab
  21. Click Add, and use these mappings

    a.   Variable Name – User::FileID

    b.   Data Type - LONG

    c.   Parameter Name – 0

  22. Click OK to save the changes to the Execute SQL Task
  23. Add a new Flat File Connection Manager, and point it to an existing flat file
  24. Right click on the Flat File Connection Manager, and select Properties
  25. Select the Expression property, and bring up the Property Expression Editor
  26. Set an expression on the ConnectionString property which makes use of the variable values retrieved from the PackageConfiguration table. Listing 18-8 provides an example of the expression.

    Listing 18-8.  Expression to Set the Path to the Input File on the Connectionstring Property

    @[User::Directory] + "\" + @[User::FileName]

Your package should now look like Figure 18-14.

9781430237716_Fig18-14.jpg

Figure 18-14. Package configured for Execute SQL Task based dynamic configurations

Setting Values using a Script Task

An alternative to retrieving your configuration with an Execute SQL Task and setting package properties through expressions is to use a Script Task. This approach can be useful if your values aren’t coming from a database, or they require additional processing logic – for example, if they are coming from an encrypted source. From within a Script, you can easily read values from external configuration files (such as an XML file), and access shared configuration resources that might be used by other, non-SSIS parts of your data integration solution. The Script Task is able to read and modify package properties at runtime, including the variable values and all connection manager properties.

Listing 18-9 provides sample code a Script Task that sets a Connection Manager’s ConnectionString at runtime.

Listing 18-9.  Sample code to set package properties using a Script Task

public void Main()
{
    // TODO: This would be set from an external configuration file
    const string SourceSystemConnectionString = " ... ";
    Dts.TaskResult = (int)ScriptResults.Success;
    if (Dts.Connections.Contains("SourceSystem"))
    {
        ConnectionManager cm = Dts.Connections["SourceSystem"];
        cm.ConnectionString = SourceSystemConnectionString;
    }
    else
    {
        // The expected connection manager wasn't found - log is and set an error status
        Dts.Events.FireError(0, "Script Task",
                                "Could not find the SourceSystem connection manager",
                                string.Empty, 0);
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}

Dynamic Package Executions

In this approach, you will use the same table from Listing 18-5, but instead of reading the configuration values with an SSIS package, you’ll use T-SQL to create dynamic package executions on the SSIS Catalog. The code in Listing 18-10 implements the following steps:

  1. Declare script variables. Note that in a real world script, these values would be set through parameters, or from an external source.
  2. Read the list of files to process from the PackageConfiguration table, and store the results in a table variable (@FileList).
  3. Loop through the list of files. For each file, the code will:

    a.   Retrieve the id and parameter values from the table variable.

    b.   Create a new SSIS Catalog package execution.

    c.   Set the parameter Directory and FileName parameter values.

    d.   Start the execution.

    e.   Update the PackageConfiguration table to mark that the file has been processed.

Listing 18-10.  Dynamic Package Execution Script

DECLARE @FolderName NVARCHAR(50) = N'ExecutionDemo'
DECLARE @ProjectName NVARCHAR(50) = N'ETL'
DECLARE @DirectoryParameter NVARCHAR(50) = N'Directory'
DECLARE @FileNameParameter NVARCHAR(50) = N'FileName'
DECLARE @PackageName NVARCHAR(100) = N'LoadCustomers.dtsx'

DECLARE @PackageList TABLE
(
  RowNum smallint,
  Id int,
  Directory nvarchar(255),
  Name nvarchar(255)
)

INSERT INTO @FileList (RowNum, Id, Directory, Name)
        SELECT ROW_NUMBER() OVER (ORDER BY id), id, Directory, Name
        FROM [dbo].[PackageConfiguration]
        WHERE processed = 0

DECLARE @maxCount int = (SELECT MAX(RowNum) FROM @FileList)
DECLARE @count int = (SELECT MIN(RowNum) FROM @FileList)

WHILE (@count < = @maxCount)
BEGIN
        DECLARE @Id NVARCHAR(255) = (SELECT Id FROM @FileList WHERE RowNum = @count)
        DECLARE @DirectoryValue NVARCHAR(255) = (SELECT Directory FROM @FileList WHERE RowNum = @count)
        DECLARE @NameValue NVARCHAR(255) = (SELECT Name FROM @FileList WHERE RowNum = @count)

        -- Create the package execution
        DECLARE @exec_id bigint
        EXEC [SSISDB].[catalog].[create_execution]
                 @execution_id = @exec_id OUTPUT,
                 @package_name = @PackageName,
                 @folder_name = @FolderName,
                 @project_name = @ProjectName

        -- Set the Directory parameter value
        EXEC [SSISDB].[catalog].[set_execution_parameter_value]
                 @execution_id = @exec_id,
                 @object_type = 20,
                 @parameter_name = @DirectoryParameter,
                 @parameter_value = @DirectoryValue

        -- Set the File Name parameter value
        EXEC [SSISDB].[catalog].[set_execution_parameter_value]
                 @execution_id = @exec_id,
                 @object_type = 20,
                 @parameter_name = @FileNameParameter,
                 @parameter_value = @NameValue

        -- Start the package execution
        EXEC [SSISDB].[catalog].[start_execution] @exec_id

        -- Return the execution ID
        SELECT N'Started package execution ' + CONVERT(nvarchar(20), @exec_id)

        -- Mark the file as processed
        DECLARE @UpdateSql nvarchar(1024) = N'UPDATE [dbo].[PackageConfiguration] SET processed = 1 WHERE id = ' + CONVERT(nvarchar(20), @Id)
        EXEC sp_sqlexec @UpdateSql

        SET @count = @count + 1
END

Summary

This chapter has covered some of the usage patterns for the new Parameter model, as well as some dynamic configuration scenarios. While the configuration patterns and best practices that were commonly used in SQL 2005 and 2008 continue to work in the latest version of SSIS, most users will see a benefit in migrating to the new model. The clarity of the Parameter model was designed to help everyone involved with an SSIS solutions life cycle, from those who develop the packages to those who deploy and schedule them.

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

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