0% found this document useful (0 votes)
81 views48 pages

CH 18

Learn how to import and export data using Data Transformation Services. Use the DTS Import Wizard to import data into SQL Server. Learn how to implement custom ActiveX scripts into your DTS packages.

Uploaded by

David Rix
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views48 pages

CH 18

Learn how to import and export data using Data Transformation Services. Use the DTS Import Wizard to import data into SQL Server. Learn how to implement custom ActiveX scripts into your DTS packages.

Uploaded by

David Rix
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Data Transformation Services (DTS)

Data Transformation Services (DTS)


Objectives
Learn how to import and export data using Data Transformation Services (DTS). Use the DTS Import Wizard to import data into SQL Server. Use the DTS Package Designer to create a DTS package. Learn how to implement custom ActiveX scripts into your DTS packages. Understand how to control package workflow using precedent constraints, transactions, and error handling.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-1

Data Transformation Services (DTS)

Introduction
Anyone who has ever been responsible for the management and administration of a database knows that the ability to import and export data is essential. Microsoft SQL Server 7.0 introduced a whole new way of performing data migrations with DTS (Data Transformation Services).

What Is DTS?
DTS can be used to import, export, and transform data between data sources. Prior to SQL Server 7.0, the only data migration tool that shipped with SQL Server was BCP (Bulk Copy Program). This tool was efficient for moving large amounts of data in and out of SQL Server. However, its DOS interface was limiting. Microsoft re-engineered BCP and integrated it into DTS with the release of SQL Server 7.0 to provide users with a much more robust data transformation tool that included a Windows interface. In SQL Server 2000, DTS has been upgraded and even more functionality has been added.

When to Use DTS


Not only can DTS be used to move data between SQL Server data sources, but it can also move data between any two OLE DB, ODBC, or text file data sources. DTS does more than simple data migration. You can use DTS to set up complex data transformations between many sources and destinations. You might want to use DTS to: Import data into your SQL Server database from a text file, Excel spreadsheet, or Oracle database. Export data from your SQL Server database to an Access, Paradox, or Sybase database. Migrate data between two heterogeneous data sources such as from Oracle to Excel or from a text file into FoxPro. Migrate data from several different data sources into your data warehouse database. Transform or validate data from your data source before inserting it into your destination.

18-2

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

The DTS Import/Export Wizard

The DTS Import/Export Wizard


DTS includes the DTS Import/Export Wizard to simplify the task of importing and exporting data. The Import/Export Wizard is a simple way to get started with DTS and should be considered for use when: You are moving data between a singe source and a single destination. You are performing a transformation that does not require a large amount of customization of the data between the source and destination. You are not comfortable with creating a DTS transformation from scratch.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-3

Data Transformation Services (DTS)

Using the DTS Import/Export Wizard


To invoke the DTS Import Wizard, right-click on the Data Transformation Services node in the Enterprise Manager tree view and select All Tasks|Import Data. The Import/Export Wizard will appear. Table 1 describes each of the screens in the wizard and its function.
Screen Name Data Transformation Services Import/Export Wizard Choose a Data Source Function This is simply a welcome screen. There is no data input on this page. Click the Next button to continue with the wizard.

This is where you select your data source. Select the type of data source from the Source drop-down list box. Some available choices are SQL Server, Microsoft Access, Excel, Sybase System 11, and text file. The rest of the options on this page will vary depending on your type of source. You may be asked to enter the user id and password for accessing your source, the file location of the source, or the specific database to be used within your source. This is where you select your data destination. This screen functions the same as the Choose a Data Source screen. This is where you specify whether your source data is coming from a specific table or is the result of a query that can pull data from several tables. If both your data source and destination are SQL Server, version 7.0 or later, you have an additional option on this page called Copy Objects Between SQL Server databases. Selecting this option allows you to copy database elements such as tables, views, users roles, and indexes between your source and destination. This screen only appears if you selected that your data will be coming from a query on the previous screen. This is where you can type the SQL query to be used for pulling your data out of the source object. You can load an existing query into the screen by clicking the Browse button and selecting the existing .SQL file. This is where you select the specific table or tables from your source to be imported into your destination. You can also select views as your source. If you are using a query as your data source, the query you entered on the previous page will show up as your only source option on this page. Clicking the Transform button for a specific source table will show you how DTS plans to perform the column mappings between the source and destination. You can modify the column mappings and the transformation script here.

Choose a Destination Specify Table Copy or Query

Type SQL Statement

Select Source Tables and Views

18-4

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

The DTS Import/Export Wizard


Save, Schedule, and Replicate Package Completing the DTS Import/Export Wizard This screen allows you to specify whether you want to run the package immediately or schedule it for a later execution. You can also specify that you want to save the wizard. This is a read-only page that provides you with a summary of the information you entered throughout the wizard. It is the last screen in the wizard.

Table 1. The screens in the DTS import wizard and their functions.

NOTE

The screens that appear in the wizard can vary slightly depending on your type of data source and destination. For example, if you are using a text file as either your source or destination, you will be presented with an additional screen in the wizard that allows you to specify the format of your text file.

Saving and Scheduling a DTS Package


Figure 1 shows the Save, schedule, and replicate package screen in the DTS Import/Export Wizard.

Figure 1. The Save, schedule, and replicate package screen in the DTS Wizard.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-5

Data Transformation Services (DTS)


This screen allows you to specify your options for saving and scheduling the execution of the wizard. You can choose to run the package immediately, schedule it for later execution, or save it without running. If you check the Schedule DTS For Later Execution check box you will have the ability to click the corresponding command button and enter your scheduling options. You can schedule the wizard to run once at a later time or you can schedule it to execute in a reoccurring fashion for a given time interval (daily, weekly, monthly, etc.). When you choose to save a DTS wizard, you have four options as to how you want the wizard saved: Save the package within SQL Server by selecting the SQL Server option. Save the package to Meta Data Services by selecting the SQL Server Meta Data Services option. If you want to save the package as an external file, you can do so by clicking the Structured Storage File option. When you save the package as an external file, the file will be saved with a .dts extension. This file can be viewed and modified at a later date through SQL Server. The final saving option for a package is to save it as Visual Basic code by selecting the Visual Basic File option. When you save the package as Visual Basic code, it is saved in an external file with a .bas extension. This file can be viewed and modified within Visual Basic or any VBA host.

Try It Out!
Here are the steps to import the authors table from the pubs database into the Northwind database using the DTS Import/Export Wizard: 1. Right-click on the Data Transformation Services node in the Enterprise Manager tree view and select All Tasks|Import Data. This launches the DTS Import Wizard opening dialog box. 2. The welcome screen appears. Click the Next button to continue. 3. This screen is where you enter information about your data source. Select the Microsoft OLE DB Provider for SQL Server since youre importing data from a SQL Server database. 4. Select the server where the pubs database resides from the Server drop-down list box. Click the Use Windows Authentication radio button if you are using integrated security. Otherwise, click the Use SQL Server Authentication radio button and enter a valid username and password for accessing the server. Select the pubs database from the Database drop-down list box and click the Next button. 18-6 Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

The DTS Import/Export Wizard


5. This brings up the dialog box to enter information about your data destination, as shown in Figure 2. Since youre importing data into a SQL Server database, make sure that Microsoft OLE DB Provider for SQL Server is selected in the Destination drop-down list box. Select the server where the Northwind database resides from the Server drop-down list box. Click the Use Windows Authentication radio button if you are using integrated security. Otherwise, click the Use SQL Server Authentication radio button and enter a valid username and password for accessing the server. Select Northwind from the Database drop-down list box and click the Next button.

Figure 2. Choosing the destination database.

6. To copy the table verbatim from the data source, check the Copy Table(s) and View(s) from the Source Database radio button. Click Next to continue.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-7

Data Transformation Services (DTS)


7. The following dialog box allows you to specify that you want to copy the authors table by checking the checkbox next to [pubs].[dbo].[authors] in the Source column, as shown in Figure 3.

Figure 3. Selecting the source and destination tables.

8. By default, DTS assumes that you want to create a new table in the Northwind database (also named authors) in which to import the data. Unless you want to rename the table, do not alter the Destination column. Click the Next button to continue. 9. The next screen is where you specify the scheduling and saving options for the wizard. To execute the transformation right away, click the Run Immediately option. To save as a package, select the Save as DTS Package and SQL Server options. Click Next to continue. 10. You will be prompted to enter a name for the DTS packagetype DTSWizardImport and select the default options. Click Next to continue.

18-8

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

The DTS Import/Export Wizard


11. The final screen is an overview of the instructions that you provided DTS on all of the previous screens. Click the Finish button to execute the transformation. Figure 4 shows the progress dialog box as the wizard completes saving the package and performing the import.

Figure 4. The DTS Import/Export Wizard executing the import and saving the package.

Once the package has been saved, it can be executed and modified from within SQL Servers DTS Designer. The next section describes working with DTS Packages from within the DTS Designer.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-9

Data Transformation Services (DTS)

DTS Packages
Once you save the data in a DTS Import or Export Wizard, it becomes a DTS package. A DTS package is an organized collection of connection objects, tasks, transformations, and workflow constraints. Packages contain one or more steps that are executed when the package is executed. Packages can be scheduled, edited, password-protected, and retrieved by version.

Key Terms
Package A program that executes one or more data transformations in addition to other tasks associated with the migration of data. Task Connection object A work item incorporated into a DTS package. A component of a DTS package that contains information about a data source. Transformation task A component of a DTS package that handles the migration of data from one data source to another.

The DTS Designer


The DTS Designer is a graphical interface developed by Microsoft to allow for easy creation and modification of DTS Packages. Not only can you use the DTS Designer to modify packages that were created from wizards, but you can also use the DTS Designer to create packages from scratch. There are several different ways to invoke the DTS Designer: To open a package saved within SQL Server, expand the Data Transformation Services node in the Enterprise Manager tree view. Expand the Local Packages node. All SQL Sever packages will appear on the right. Double-click the desired package to open it with the DTS Designer. To open a package saved as an external file, right-click on the Data Transformation Services node in the Enterprise Manager tree view and select Open Package from the menu. Locate the file in the Select File dialog box and click Open. To create a new package from scratch, right-click on the Data Transformation Services node in the Enterprise Manager tree view and select New Package from the menu. Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-10

DTS Packages
NOTE The DTS Designer is simply a graphical interface for writing to the DTS COM components. It is possible to create DTS packages from scratch using any programming language that supports COM such as Visual Basic or C++. DTS packages created programmatically in this way bypass the use of the DTS Designer. Additional programming skills are required for creating DTS packages programmatically, but programmatically created packages can provide additional control and flexibility that is not available when you create a package using the DTS Designer.

DTS Package Components


Figure 5 illustrates a simple DTS package shown within the DTS Designer. The different components and tasks each have a different icon, or line type to distinguish them from each other. The toolbar on the left side of the screen shows DTS Designer-specific actions.

Figure 5. A DTS package shown within the DTS Designer.

A DTS package typically consists of one or more combinations of the following three types of components: Connection objects: A connection object is a built-in component that allows you to establish a connection to a data source or destination. A 18-11

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Data Transformation Services (DTS)


connection object can be added to your package by clicking on it in the Data section of the toolbar in the DTS Designer. Transformations tasks: A transformation performs a migration from a data source to a destination. You can add a transformation to your package by selecting the connection objects that represent both your data source and destination and then clicking the Transform Data Task icon from the Task section of the toolbar. Other built-in DTS tasks: In addition to transformation tasks, there are several other tasks that are built into DTS. You can add a built-in task to your project by selecting it from the Task section of the toolbar in the DTS Designer. Each built-in task is designed to execute a specific function such as executing a custom ActiveX script or using FTP to download data from a remote location

Each of these three types of components is discussed in detail in the following sections.

Connection Objects
Connection objects are used to gather information about your data source or destination. You must have at least one connection object in your package in order to perform a data migration using a transformation task. The Data section of the toolbar in the DTS Designer lists all of the available types of connection objects. Once you have added a connection object to your package by clicking on it in the toolbar, you have the opportunity to set its properties. You can also set or modify the properties for a connection object after adding it to your package.

Try It Out!
Follow these steps to view the Connection properties for the package the wizard created: 1. Select the DTSWizardImport package in the Local Packages window. Right-click and choose Design Package from the menu.

18-12

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

DTS Packages
2. Right-click on Connection1 and select Properties from the menu. This loads the Connection Properties dialog box, as shown in Figure 6.

Figure 6. The Connection Properties dialog box.

3. Click OK and display the properties for Connection2. All of the information in the Connection1 and Connection2 Properties dialog boxes was initially collected by the wizard.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-13

Data Transformation Services (DTS)

Connection Object Properties


The properties that need to be set vary depending on the type of connection object. The Connection toolbar shows you the different types of connections available. You can see what the icons represent by its ToolTip, as shown in Figure 7.

Figure 7. The Connection toolbar.

18-14

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

DTS Packages
The different connection objects found on the toolbar are described in Table 2.
Type of Connection Microsoft OLE DB provider for SQL Server Microsoft Access Description Uses OLE DB to establish a connection to a SQL Server database. Properties include the server name, login name and password, and the database name. Establishes a connection to an Access MDB file. Properties include the path and file name of the MDB and the user name and password. Establishes a connection to an Excel XLS file. Properties include the path and file name of the XLS and the user name and password. Establishes a connection to a dBase 5 file. Properties include the path and file name of the dBase 5 file and the user name and password. You can also connect to a dBase III or IV file by making the appropriate selection from the data source drop-down list box in the Connection Properties dialog box. Establishes a connection to an HTML file to be used as a data source. Properties include the path and file name of the file and the user name and password. Establishes a connection to a Paradox 5.x file. Properties include the path and file name of the Paradox 5.x file and the user name and password. You can also connect to a Paradox 3.x or 4.x file by making the appropriate selection from the data source drop-down list box in the Connection Properties dialog box. Establishes a connection to a text file. Properties include the path and file name of the text file. Advanced properties include specifying whether the file is fixed length or delimited. You can further specify the fixed length for each column or the characters that represent row and column delimiters. Establishes a connection to an Oracle database using an ODBC driver. Properties include the server name and user name and password. Establishes a connection to a data source using a Microsoft Data Link. You can either load an existing UDL or define a new data link using an OLE DB provider. Establishes a link to a data source using an ODBC driver. Properties include the name of the DSN and the user name and password.

Microsoft Excel

DBase 5

HTML file (source) Paradox 5.x

Text file source/destination

Microsoft ODBC driver for Oracle Microsoft Data Link Other connection

Table 2. The types of connection objects that are available on the DTS Designer toolbar.

In addition to the types of connection objects that are available on the toolbar, there are also a few other types of connection objects. These include Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-15

Data Transformation Services (DTS)


connection objects for the Microsoft OLE DB provider for OLAP Services and Microsoft FoxPro VFP driver. To create a connection object of a type not listed on the toolbar, add a connection object of any type to the package, and then open the Connection Properties dialog box and select the desired connection type from the Data Source drop-down list box.

The Transform Data Task


When you add a transformation task to a DTS Package, you are creating a call to the DTS data pump. The data pump fetches data rows from a source connection and writes data rows to a destination connection. Before you can add a transformation task to your project, the data source and data destination connection objects must already exist.

Try It Out!
Follow these steps to view the transformation task for the package the wizard created: 1. Select the arrow connecting Connection1 and Connection2 in the Designer window. Right-click and choose Properties from the menu. This loads the Properties dialog box.

18-16

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

DTS Packages
2. The dialog box has five different tabs to show the options available for the transformation task, which are described in Table 3. Click the Transformations tab. This displays the transformations for the task, as shown in Figure 8.

Figure 8. The Transformations tab.

3. Right-click on the join line between the Source and the Destination and choose Edit from the menu. This allows you to further modify the properties of the task. Click OK or Cancel when finished.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-17

Data Transformation Services (DTS)


Table 3 describes the options available on the five tabs in the Transform Data Task Properties dialog box.
Tab Source Description This tab lists your source object. By default it is linked to the connection object you specified as your source when you added the transformation. Here is where you can specify which table from your source you will be pulling data from. If your data source supports queries, you can also specify a query to be used to retrieve data from your source. This tab lists your destination object. By default it is linked to the connection object you specified as your destination when you added the transformation. Here is where you can specify which table in your destination you will be inserting data into. This tab lists the column mappings between your source and destination. To delete an existing column mapping, select it and click the Delete button. To add a new mapping, select the column or columns from your source. Then select the column or columns from your destination that you wish to map to and click the New button. This tab allows you to create lookup queries to be associated with this transformation. Lookup queries are predefined, parameterized queries that can be dynamically executed during a transformation if you write a custom transformation ActiveX script. This tab is where you specify the advanced options for your transformation. These options include defining an exception file for error handling and specifying the batch size for the import.

Destination

Transformations

Lookups

Options

Table 3. The five tabs on the Transform Data Task Properties dialog box.

Creating a Package from Scratch


You dont have to use the DTS Import/Export Wizard to create a packageyou can easily create one yourself from scratch.

Try It Out!
Here are the steps to create a simple package that imports the titles table from the pubs database into the Northwind database. This is essentially the same task you accomplished before with the Import Wizard, but this time youll do it without the aid of the wizard and youll import a different table.

18-18

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

DTS Packages
1. Expand the Data Transformation Services node in the Enterprise Manager. Right-click on Local Packages and select New Package from the menu. The DTS Designer loads with an empty screen. 2. Click the Microsoft OLE DB Provider for SQL Server icon in the Data toolbar to add a Connection object to the package to represent the data source. This opens the Connection Properties dialog box. 3. Type Pubs as the name of the connection. Enter the information about the data source by selecting the appropriate authentication information for your server. Select pubs from the Database drop-down box. Click OK. This adds a new connection to the Designer window. 4. Repeat step 2 to add a connection object to represent the data destination. This time name the connection Northwind. Make sure Microsoft OLE DB Provider for SQL Server is selected in the Server drop-down list box. And that Northwind is selected from the Database drop-down box. Click OK. This adds a second connection to the Designer window. 5. Click on the Pubs Connection object. Hold the SHIFT key down, and click on the Northwind Connection object. Both connection objects should be highlighted. 6. Click on the Transform Data Task button in the Task section of the toolbar shown in Figure 9. This adds a transformation going from the Pubs connection to the Northwind connection. The transformation task is represented by a gray arrow.

Figure 9. The Task section of the Designer toolbar.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-19

Data Transformation Services (DTS)


7. Right-click on the gray arrow and select Properties from the menu. Type Migrate Titles to Northwind on the Source tab as the description. Select [pubs].[dbo].[titles] from the Table/View dropdown list box to do a straight copy, as shown in Figure 10.

Figure 10. Naming the connection and specifying the source table.

18-20

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

DTS Packages
8. Click the Destination tab. Since the Titles table doesnt exist in the Northwind database, you need to have DTS create one. Click the Create button, which loads the dialog box shown in Figure 11. This is the script that will be used to create the table in the destination database. Click OK.

Figure 11. Creating the destination table.

9. Click the Transformations tab. By default, DTS assumes that you are doing a straight column copy from the source to the destination. If you want to change any of the column mappings, double-click the join line between them. Otherwise, click OK to complete the task. 10. Click the Save toolbar button or choose Package|Save from the menu. Type Titles to Northwind in the Package Name text box and fill in the other properties. Click OK. This saves the package. 11. Run the package by clicking the Execute toolbar button or selecting Package|Execute.

Other Built-In Tasks


Unfortunately, most advanced data migration tasks are not as simple as pulling data from a source and inserting it into a destination. Often there are other tasks that need to be done either prior to, during, or after a data migration in order to ensure that the migration is successful. With this in mind DTS has several built-in tasks that can be added to your project for further processing. These built-in tasks are listed in the Task section of the toolbar in the DTS Designer, as shown earlier in this chapter in Figure 9.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-21

Data Transformation Services (DTS)


To add a built-in task to your project, click on it on the toolbar. Table 4 lists each of the built-in tasks and describes its functionality.
Task File Transfer Protocol ActiveX Script Description This task allows you to use FTP to download data files from the Internet or a remote server. With this task you can use an ActiveX scripting language to write functions to perform any additional processing that may be necessary. Using this task you can run an executable program or a batch file as a task. This task proves an easy way to execute a SQL statement. With this task you can perform various Insert, Update, Delete SQL statements or stored procedures. Which SQL statement you executed can be determined dynamically as the package is executed based on your source data. This task allows you to copy elements from one instance of SQL Server to another. In addition to data you can copy tables, stored procedures, constraints, indexes, etc. This task uses MAPI to send e-mail notifications such as an e-mail notifying the database administrator when a DTS package fails to execute properly. Using this task is the most efficient way to copy very large quantities of data into SQL Server. With this task you can execute another DTS package as part of your package workflow. This task allows you to send and receive messages between DTS packages using Message Queuing. With this task you can transfer error messages between SQL Server databases. This task allows you to transfer databases between SQL Server database servers. Using this task allows you to transfer stored procedures between SQL Server databases. This task allows you to transfer jobs between SQL Server databases. With this task you can transfer login information between SQL Server databases. This task allows you to set properties for the objects in your package dynamically at run time. Typically the values for the properties come from a location outside of DTS such as an INI file.

Execute Process Execute SQL Data Driven Query

Copy SQL Server Objects Send Mail

Bulk Insert Execute Package Message Queue Transfer Error Messages Transfer Databases Transfer Master Stored Procedures Transfer Jobs Transfer Logins Dynamic Properties

Table 4. The built-in tasks that are available in the DTS Designer.

18-22

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow

Controlling DTS Workflow


By default, DTS attempts to execute a specified number of transformation and tasks synchronously or at the same time. However, you can control the workflow of your package and the execution of tasks within the package. Controlling the workflow of an application is done primarily through the implementation of precedent constraints, transactions, error handling, and custom ActiveX scripting.

Precedent Constraints
Precedent constraints allow you to specify the tasks to be executed sequentially. There are three kinds of precedent constraints. They are listed in Table 5.
Constraint Type On success On failure On completion Description Specifies that the first task must complete successfully before the second task can begin. Specifies that the first task must fail to complete successfully before the second task can begin. Specifies that the first task must complete before the second task can begin. The second task will execute regardless of whether the first task succeeded or failed.

Table 5. The three types of precedent constraints in DTS.

To add a precedent constraint between two tasks, select the task you want to execute first. Holding down the Shift key, select the task you want to execute second. Then select On completion, On success, or On failure from the Workflow menu depending on which of the three types of precedent constraints you wish to create.

DTS Transactions
Without transactions, there is no way to undo transformations that have already completed successfully if an error occurs later in the package execution. When transactions are implemented, they allow you to group package steps together so that they can be committed or rolled back as a unit. There are two rules to remember when implementing transactions. First, in order for transactions to work with DTS you must use a data provider that supports transactions. Second, only one transaction can be active at a time within DTS. Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-23

Data Transformation Services (DTS)


Before you can implement transactions you must make sure that they are enabled for your package (this is the default). To verify that transactions are enabled for your package, open it in Design view in the DTS Designer and right-click anywhere in a blank area. Select the Package Properties from the menu and select the Advanced tab. Check the Use Transactions check box to enable transactions in your package. Once transactions are enabled, you can add a transformation to a transaction. Right-click on the transformation (the gray arrow) and select Workflow Properties. Select the Options tab to configure transactions, as shown in Figure 12.

Figure 12. Configuring transactions.

18-24

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow


Table 6 describes the available options.
Check Box Join transaction if present Commit transaction on successful completion of this step Roll transaction back on failure Description When this box is checked, the transformation is automatically added to the active transaction if transactions are enabled in the package. When this box is checked, all of the transformations involved in this transaction will be committed once this transformation successfully completes. DTS will automatically begin a new transaction after committing the current one. When this box is checked, all of the transformations involved in this transaction will be rolled back if this transformation fails. DTS will automatically begin a new transaction after rolling the current one back.

Table 6. Options available when adding a transformation to a transaction.

Error Handling
As your packages begin to get more robust, the ability to track errors or problems that may occur becomes increasingly important. DTS provides you with the ability to create package logs and exception files to help you handle any errors that may arise.

Package Logs
Package logs provide an overview of all the steps in a package. Each step in the package is recorded in the log. Since any information as to whether or not the step executed successfully and any errors that might have been encountered are also recorded, the package log can be helpful in determining where and why a package error may have taken place. Follow these steps to generate a package log in the DTS Designer: 1. Make sure no items are selected in the designer window, and choose Package|Properties from the menu. Click the Logging tab of the Package Properties dialog box. 2. If you would like your package log information to be stored in an external file, enter the name and path of the external file in the Error File text box. 3. If you would like your package log information to be stored within SQL Server, check the Log Package Execution to SQL Server check box. Click OK when finished.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-25

Data Transformation Services (DTS)

Exception Files
Exception files can be generated as a data transformation is taking place. The purpose of the execution file is to provide specific information on any row of data that encounters a problem when attempting to migrate it into the data destination. Follow these steps to add an exception file to a transformation task: 1. Right-click on the transformation task and select Properties. 2. Click the Options tab and enter the name of the exception file in the Name text box. 3. Select the file type and file formatting options from the File Type and File Format sections and click OK when finished.

Custom ActiveX Scripts


To further enhance DTSs capabilities, Microsoft has provided the ability to integrate the full power of either the VBScript or JScript scripting language into any one of three locations within a DTS package. These three script locations, or kinds of scripts, are transformation scripts, workflow scripts, and ActiveX scripting tasks. Integrating custom ActiveX scripts into a package will have a slightly negative impact on the performance time of your package, but they can add crucial functionality.

Global Variables
Before discussing the three types of ActiveX scripts, its important to discuss the role of global variables in DTS Packages. Global variables allow you to store data and object references that can be passed between different types of scripts. There are two kinds of global variables: those declared at design time and those declared programmatically at run time. Global variables that are created at design time have the ability to retain their values in between package executions. This can be crucial if you need to carry data from one package execution to the next. Follow these steps to create a global variable at design time: 1. Right-click anywhere in the DTS Designer and select Package Properties. 2. Click the Global Variables tab. Any existing global variables will be displayed along with their data type and current value. 3. Click the New button and type in the name of the global variable, select its database, and enter a starting value (optional).

18-26

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow


Its usually a good idea to click on the Explicit Global Variables check box on the Global Variables tab of the DTS Package Properties dialog box. If this box is checked, you will receive an error in your ActiveX scripts if you attempt to access a global variable that hasnt been previously created. If you leave this box unchecked, you will not receive an error in your scripts when you attempt to reference a global variable that hasnt been previously created. Instead, a new global variable will be created on the spot with the new name. This could lead to undesired errors in your script. To reference global variables in any of the three types of ActiveX scripts, use a custom collection in DTS called DTSGlobalVariables. This collection contains all of the global variables in your package. The following line of code written in VBScript updates a global variable called current_date with the current system date.
DTSGlobalVariables(current_date).value = Date

Transformation Scripts
The first script location or kind of script is the transformation script. A transformation script is executed as data is being moved from one location to another.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-27

Data Transformation Services (DTS)


There are several kinds of transformation scripts that are executed at different times during the transformation process. Table 7 lists the different types of transformation scripts.
Script Type Pre source data Description The script is executed once, prior to the first row of data being retrieved from the source. This script can be used to do things such as initialize files, write our header rows, or initialize global variables. The script is executed once for each row of data in the source after it is received from the source and before it is inserted into the destination. This script can be used to validate or scrub data, combine fields, or change the data format. The script is executed once for each row of data in the source. It follows the Row transform script if there is one. There are three subtypes for this script. The first, On Transform Failure executes if a failure occurs during the transformation. It provides you with a chance to examine the data, see why the failure occurred, and possibly fix the problem. It is followed by one of the next two subtypes. The On Inset Success subtype executes if the insert of the data is a success. The On Insert Failure script executes if the data is not inserted in the destination. The script executes once at the end of every batch of records inserted into the destination. The script executes only once after all the records in the source have been processed. The script executes only once at the end of the transformation task. The destination data is accessible in this script. This task can be used to deallocate memory or write out footer rows.

Row transform

Post row transform

Batch complete Pump complete Task complete

Table 7. These are the different types of transformation scripts.

Follow these steps to add a new script to your transformation task: 1. Right-click on the transformation task and select Properties. 2. Click the Transformations tab. 3. Click the New button. The Create New Transformations dialog box appears. 4. Select ActiveX script and the type of transformation and click OK. 5. Use the Transformation Options dialog box to set the options for the script. Use the Source and Destination columns to select the column or columns from the data source and destination that you will be working with in your script. On the Phases tab, select which phase in 18-28 Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow


the transformation you want your script to execute (i.e., the type of transformation script). When you are through setting the options, click the Properties button on the General tab. 6. Enter your script in the ActiveX Script Transformation Properties dialog box. Use the Browse button on this screen to search for and load a script file that was written using another editor. Use the Save button to save your script as an external file. Writing the actual transformation script is just like writing any other piece of code using a scripting language, except for a couple of unique characteristics. The first unique characteristic is that there are four custom collections that are available for use in your transformation scripts. These collections are listed below: DTSGlobalVariables: The collection of global variables that exist in your package. DTSSource: The collection of columns in your source data that you have selected to be available in this script. DTSDestination: The collection of columns in your destination data that you have selected to be available for this script. DTSLookups: A collection of lookup, or predefined parameter queries that can be run during your transformation script. You define these queries at design time on the Lookups tab of the Transformation Data Task Properties dialog box.

The second unique characteristic of transformation scripts is that they are contained within functions. These functions must return specific values called return codes. Return codes tell DTS how to proceed after running this script. The return codes are referenced by constants.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-29

Data Transformation Services (DTS)


There are several return code constants available for transformation scripts. Table 8 lists some of the options and explains the effect they have on the processing of the data.
Return Code DTSTransformStat_OK Description The processing of this row has been a success. The row should be inserted into the destination and DTS should move on to the next row of data. The processing of this row has not been a success. The row should not be inserted into the destination and DTS should move on to the next row. Continue processing with this row of data instead of retrieving a new row from the source. This status can be useful if you are inserting data from one row in your source into many rows in your destination. The processing of this row has not been a success. No further processing of this row should be done and DTS should not attempt to process any more rows from the source.

DTSTransformStat_SkipRow

DTSTransformStat_SkipFetch

DTSTransformStat_AbortPump

Table 8. A sample of the return code constants available for transformation scripts.

NOTE

If you are not familiar with how to write code using either VBScript or JScript, you will have to gain a basic understanding of the programming languages involved before you will be able to write effective scripts in DTS.

The following code example written in VBScript is an example of a Row Transform transformation script. In this example, the data source stores a customers name in two fields, first_name and last_name. However, in the destination, there is only a single customer_name field that stores both the first and last name. The script combines the first_name and last_name fields to create the data to be inserted in the customer_name field in the destination.
Function Main() Dim NewName NewName = DTSSource(first_name) & " " & _ DTSSource(last_name) DTSDestination(customer_name) = NewName Main = DTSTransformStat_OK End Function

18-30

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow


In the following code example, the create_date field for the source record is checked to see if the record was created in the source during the current month. If not, then the record is not inserted into the destination.
Function Main() If Month(DTSSource(create_date)) <> Month(Date) _ Then Main = DTSTransformStat_SkipRow Else Main = DTSTransformStat_OK End If End Function

Workflow Scripts
The second kind of script is the workflow script. A workflow script is associated with a transformation task and runs prior to the transformation task. This gives you the ability to do things such as verify external conditions and dynamically decide whether or not to cancel the transformation task prior to its execution. Unlike transformation scripts that have four custom collections, workflow scripts only have access to one custom collection, the DTSGlobalVariables collection. That means that the global variables for your package can be accessed and manipulated by workflow scripts. Similar to transformation scripts, workflow scripts are contained within functions and must pass back a specific return code. The return code values available for workflow scripts are different from the return code values that are available for transformation scripts. Table 9 lists the only three return codes that are valid for workflow scripts.
Return Code DTSStepScriptResult_ExecuteTask DTSStepScriptResult_DontExecuteTask Description This return code instructs DTS to proceed with the associated transformation task. This return code instructs DTS to skip over the associated transformation task without executing it. This script instructs DTS to loop back to the beginning of the workflow script. The resulting loop will continue until one of the other two return codes is passed. This looping buys you time to do things such as complete external processing.

DTSStepScriptResult_RetryLater

Table 9. The return code constants that are available for workflow scripts.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-31

Data Transformation Services (DTS)


Follow these steps to associate a workflow script to your transformation task: 1. Right-click on the transformation task and select Workflow Properties. 2. Click the Options tab. 3. Check the Use ActiveX Script check box. 4. Click the Properties command button. 5. Enter your script in the Workflow ActiveX Script Properties dialog box. 6. Click the OK button. In the following example, a check is done to see if the Microsoft Access Biblio database exists in the designated location. If the file exists, the associated transformation task will be executed. If it doesnt, then the associated transformation task will not be executed:
Function Main() Dim fso

Set fso = CreateObject("Scripting.FileSystemObject") IF NOT(fso.FileExists("C:\Biblio.mdb")) THEN DTSStepScriptResult_DontExecuteTask ELSE Main = DTSStepScriptResult_ExecuteTask END IF End Function

ActiveX Scripting Task


The third and final type of ActiveX script is the ActiveX scripting task. This is a separate task whose sole function is to run any script you write. This makes the possible jobs that can be accomplished by an ActiveX script almost limitless. Typically, ActiveX scripting tasks are used to do things such as access external COM objects, use ADO to communicate with other data sources, or write to external log files. Similar to workflow scripts, the only custom collection available in an ActiveX scripting task is the DTSGlobalVariables collection. And just like transformation and workflow script, ActiveX scripting task scripts are contained within functions and must pass back a specific return code. 18-32 Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow


There are only two valid return codes for ActiveX scripting task scripts: DTSTaskExecResult_Success. This return code indicates that the script executed successfully. DTSTaskExecResult_Failure. This return code indicates that the script did not execute successfully.

Follow these steps to add an ActiveX scripting task to your project: 1. Click the ActiveX scripting task icon in the Task section of the DTS Designer toolbar. 2. Enter your script in the ActiveX Script Task Properties dialog box. 3. After you have added the task to your project, you can view or modify the script by double-clicking on the ActiveX script task.

Try It Out!
Here are the steps to enhance the Titles to Northwind package by adding an ActiveX scripting task to write to an external log file. You will also add a precedent contraint so that the ActiveX task only executes after the transformation is completed successfully: 1. Open the saved Titles to Northwind package in the DTS Designer. 2. Click the ActiveX Script Task icon in the Task section of the toolbar to add an ActiveX script task. This loads the ActiveX Script Task dialog box. 3. Enter Log info to file in the Description text box for this task.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-33

Data Transformation Services (DTS)


4. Enter the following VBScript code in the scripting window:
Function Main() Dim fso Dim file Set fso = _ CreateObject(Scripting.FileSystemObject) Set file = fso.CreateTextFile(c:\DTSLog.txt) file.WriteLine(The Titles to Northwind package _ completed successfully.) file.close Main = DTSTaskExecResult_Success End Function

18-34

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow


5. The dialog box should look like the one shown in Figure 13. Click the Parse button to check the syntax, and then the OK button to close the dialog box.

Figure 13. Creating an ActiveX scripting task.

6. Click on the Northwind connection object, hold down the SHIFT key, and click on the ActiveX script task. Both the connection object and the task should be highlighted.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-35

Data Transformation Services (DTS)


7. Select Workflow|On Success from the menu. This adds the precedent constraint between the completion of the transformation task and the ActiveX script task, as shown in Figure 14. Save and execute the package.

Figure 14. Setting the scripting task to execute on success.

18-36

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow

Summary
DTS can be used to set up simple or complex data transformations between any two OLE DB, ODBC, or text file objects. The DTS import and export wizards are a good way to perform single step transformations. Using the wizards is also a good way to start building your package if you are not comfortable with the DTS Designer. DTS packages can be saved and scheduled to run at specific time intervals. DTS packages are usually made up of a combination of one or more of the following three elements: connection objects, transformation tasks, and other built in DTS tasks. Adding precedent constraints to your package allows you to enter conditional logic into your package and enforce the sequential execution of tasks. Implementing transactions in your DTS packages allows you to group transformation tasks together so that they can either be committed or rolled back as a group. You can generate package logs and exception files to help track any errors that may occur in your package. Writing custom ActiveX scripts allows you to validate, transform, and scrub your data along with adding custom code for additional processing. There are three locations or types of ActiveX scripts: transformation, workflow, and ActiveX scripting tasks. Global variables allow you to pass data between your ActiveX scripts.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-37

Data Transformation Services (DTS)

(Review questions and answers on the following pages.)

18-38

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow

Questions
1. True/False: SQL Server must be either your data source or destination when using DTS to migrate data. 2. What is the function of a connection object? 3. What is the function of a transformation task? 4. True/False: Using a precedent constraint allows you to specify that a certain task should only be executed if another task fails to execute properly. 5. What are the three types or locations of custom ActiveX scripts in a DTS package? 6. Name one of the important things that can be accomplished with transformation script. 7. True/False: All Global variables lose their values once a package has finished executing.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-39

Data Transformation Services (DTS)

Answers
1. True/False: SQL Server must be either your data source or

destination when using DTS to migrate data.


False. DTS can be used to migrate data between any two OLE DB, ODBC, or text file data sources.

2. What is the function of a connection object?


Connection objects are used to gather information about your data source or destination.

3. What is the function of a transformation task?


A transformation performs a migration from a data source to a destination.

4. True/False: Using a precedent constraint allows you to specify that a certain task should only be executed if another task fails to execute properly.
True. You can use a precedent constraint to specify that one task should only be executed after another task completes either successfully or unsuccessfully, completes successfully, or fails to complete successfully. 5. What are the three types or locations of custom ActiveX scripts in

a DTS package?
Transformation, workflow and Active X scripting task

6. Name one of the important things that can be accomplished with transformation script.
Some of the things that transformation scripts can be used for are: validate data before inserting it into your destination, change the format of your source data, combine several fields from your source into one field for your destination and executing lookup queries.

7. True/False: All Global variables lose their values once a package has finished executing.
False. Global variables declared at design time can retain their values between package executions.

18-40

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Controlling DTS Workflow

Lab 18: Data Transformation Services (DTS)


TIP: Because this lab includes a great deal of typed code, weve tried to make it simpler for you. Youll find all the code in DTSLab.TXT, in the same directory as the sample project. To avoid typing the code, you can cut/paste it from the text file instead.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-41

Lab 18: Data Transformation Services (DTS)

Lab 18 Overview
In this lab youll learn how to create a DTS package using the DTS Designer that will export data out of the authors table in the pubs database to a text file. Once the export has completed successfully, you will use an ActiveX scripting task to write a header line to the newly created text file. To complete this lab, youll need to work through two exercises: Setting Up a Transformation Task Adding an ActiveX Scripting Task

Each exercise includes an Objective section that describes the purpose of the exercise. You are encouraged to try to complete the exercise from the information given in the Objective section. If you require more information to complete the exercise, the Objective section is followed by detailed step-bystep instructions.

18-42

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Setting Up a Transformation Task

Setting Up a Transformation Task


Objective
In this exercise, youll create a new package, and add two connection objects, one for the source (the authors table in the pubs database) and one for the destination (c:\AuthorList.txt). Youll then add the transformation task to copy the data from the authors table to the destination.

Things to Consider
How do you launch the DTS Designer? How do you create a new package? How do you add connections and tasks?

Step-by-Step Instructions
1. Expand the Data Transformation Services node in the Enterprise Manager. Right-click on Local Packages and select New Package from the menu to load the DTS Designer. 2. Click the Microsoft OLE DB Provider for SQL Server toolbar button in the Data section of the toolbar. This adds a connection object for the data source and loads the Connection Properties dialog box. Choose the following options: New Connection: Data Source: Server: Authentication: Database: Pubs Microsoft OLE DB Provider for SQL Server (local) or your server name Either SQL Server or NT pubs

Click OK when finished. 3. Click the Text File (Destination) button in the Data section of the toolbar. This adds a connection object for the destination and loads the Connection Properties dialog box. Choose the following options: New Connection: Author List Data Source: Text File (Destination) File Name: c:\AuthorList.txt Microsoft SQL Server 2000 Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-43

Lab 18: Data Transformation Services (DTS)


If this text file doesnt already exist, it will be automatically created the first time your run the package. 4. Click the Properties button and accept all of the default values. Click Finish and then OK. You now have two connection objects. 5. Click on the Pubs connection object, hold down the SHIFT key, and select the Author List connection object so that both are highlighted. 6. Click the Transform Data Task button in the Task section of the toolbar to add a transformation from the Pubs connection to the Author List connection. You should see the transformation task represented by a gray arrow, as shown in Figure 15.

Figure 15. Add a transformation task between Pubs and Author List.

7. Right-click on the arrow and select Properties from the menu. On the Source tab enter Migrate Authors to Text File as the description for this task. 8. Select [pubs].[dbo].[authors] from the Table/View drop-down list box and click the Destination tab. 9. Accept the default definitions and click Execute.

18-44

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Setting Up a Transformation Task


10. Click the Transformations tab. By default, DTS assumes that you are doing a straight column copy from the source to the destination. Click OK to close the properties dialog box. Click the Save button or choose Package|Save from the menu to save the package. 11. Type DTS Lab for the Package name setting in the Save DTS Package dialog box. Enter the appropriate server name and authentication settings, as shown in Figure 16. Click OK when finished.

Figure 16. Saving the DTS package.

12. Click the Execute button or choose Package|Execute from the menu to execute the package. The AuthorList.txt file will show up on your c:\ drive.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-45

Lab 18: Data Transformation Services (DTS)

Adding an ActiveX Scripting Task


Objective
In this exercise, youll create an ActiveX script task to add a footer row to AuthorList.txt that will record the date the export was done and the name of the database where the records originated. Youll then add a precedent constraint so that the ActiveX scripting task is only executed after the transformation task has completed successfully.

Things to Consider
How do you add an ActiveX script task? How do you retrieve the current date? How do you add a precedent constraint?

Step-by-Step Instructions
1. Open the package in the DTS Designer if it is not already open. Click the ActiveX Script Task button in the Task section of the toolbar to add an ActiveX script task. This loads the ActiveX Script Task Properties dialog box. 2. Enter Add footer file in the Description textbox.

18-46

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

Adding an ActiveX Scripting Task


3. Enter the following function in the scripting window. The dialog box should look like the one shown in Figure 17.
Function Main() Dim fso Dim file Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile("c:\AuthorList.txt", 8) file.WriteLine("This file was created on " & Date) file.WriteLine("These records are from the Author " _ & " table in the Pubs database") file.Close Main = DTSTaskExecResult_Success End Function

Figure 17. Creating the script to add the footer.

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

18-47

Lab 18: Data Transformation Services (DTS)


4. Click the Parse button to make sure the syntax is okay, and then click OK. 5. Next you need to add the precedent constraint. Click on the Author List connection object, hold down the SHIFT key, and click on the Add footer file ActiveX script task so that both are highlighted. 6. Choose Workflow|On Success from the menu. This adds the precedent constraint between the completion of the transformation task and the ActiveX script task and is represented by a green and white striped arrow. Save and execute the package. 7. Open the c:\AuthorList.txt file in Notepad. Youll see the new lines added at the end, as shown in Figure 18.

Figure 18. The completed c:\AuthorList.txt export.

18-48

Microsoft SQL Server 2000 Professional Skills Development


Copyright by Application Developers Training Company and AppDev Products Company, LLC All rights reserved. Reproduction is strictly prohibited.

You might also like