Home > Articles > Data > SQL Server

  • Print
  • + Share This
Like this article? We recommend

Developing DTS Packages

To illustrate the process of developing a DTS package, we'll use the sample Northwind database included with SQL Server 2000.

Suppose a Northwind customer in France asks the Northwind database administrator (DBA) to provide the following:

  • He wants a catalog of all products with all the prices in Euros.

  • He needs the product ID and the product description fields to be merged.

  • He wants to load the catalog into a proprietary database system, using a flat text file.

The Northwind database administrator will use a DTS package to extract, transform, and export the desired data into the desired format.

Assuming that you are the Northwind DBA, here's how you'd do all that.

Creating the Flat Text File

You'll begin by creating the flat text file, into which you'll load the transformed data. To create the flat text file, you need to use a source Connection object to read data from the SQL Server database, a destination Connection object to write the data to a text file, and a Transformation object to read only the needed fields and make the required calculations:

  1. To create the package, open SQL Server Enterprise Manager, expand Data Transformation Services under the desired server, and select Local Packages.

  2. The Data Transformation Services are common to all the databases stored in the server. This means that a single DTS package can access the data from one or more databases. When you select Local Packages, you get a list of all the packages available on this server.

  3. To create a new package, open the Action menu and select New Package. The result is an empty DTS Designer.

  4. Every DTS package needs source and destination Connection objects. The destination connection can use the same database as the source connection; a different database; a different location (text files, spreadsheets, third-party databases, and so on); or a Microsoft Analysis Services OLAP database. For this example, in the left pane under Connection, click the Microsoft OLE DB Provider for SQL Server icon.

  5. In the Connection Properties dialog box, select New Connection and enter the name Products Catalog in the text box. Then select Windows Authentication, select the Northwind database from the database list, and click OK.

  6. Now that you have a connection to the Northwind database, you need to create a connection to a text file. In the left pane under Connection, click the Text File (Destination) icon. Select New Connection and enter the name Text Catalog in the text box. Enter the filename c:\catalog.txt. (This is the file to which the DTS package will export the data.)

  7. NOTE

    The Properties button opens a dialog box in which you can specify the format options of the text file. If you don't click the Properties button, the text file properties dialog box opens automatically when you click OK.

    You should now have two connection objects in the DTS Designer window. You can arrange the objects by dragging them to any position.

  8. The next step is to create a Transform Data Task to read the desired data from the Northwind database, make the necessary changes, and send the data to the destination Connection object. To create a Transform Data Task between the two connections, select both connection objects; in the left pane under Task, select Transform Data Task. A task arrow appears between the source connection and the text file connection (see Figure 2).

  9. Figure 2Figure 2 The DTS package.

  10. Now you need to edit the Transform Data Task properties to define the desired fields from the source database, the fields for the destination text file, and the mappings between them. For this example, because you're using a single table from the source database, you can simply tell the Transform Data Task which table to use. (If you need to use several tables, you can enter a T-SQL statement.) Right-click the task arrow and select Properties to display the Transform Data Task properties (see Figure 3).

  11. Figure 3Figure 3 The Transform Data Task Properties dialog box.

  12. In the Description text box, enter Export Catalog. In the Table/View list, select the table [Northwind].[dbo].[Products].

  13. Click the Destination tab. The Define Columns dialog box appears, in which you can specify the datatypes for each column or allow SQL Server to create the mapping for you. To allow SQL Server to do the mapping, click the Populate From Source button.

  14. Click the SupplierID field, click the down-arrow button to the right of the field name, and click Ignore. The Northwind client doesn't need this information. Repeat this step for the CategoryID, UnitsInStock, UnitsOnOrder, ReorderLevel, and Discontinued fields.

  15. Click Execute.

Merging Fields from the Database

Because the Northwind customer requested a field that's not already defined in the Northwind database (a merged product ID and product description), you need to modify the field mappings created by the Transform Data Task. You have to merge the ProductID and ProductName fields from the Northwind database and then copy this information to the ProductID field in the destination text file.

To accomplish this task, you can use an ActiveX script. ActiveX scripting is very similar to VBScript; the main difference is that it incorporates statements to reference the different objects of the DTS package. For this example, you'll use DTSSource, DTSDestination, and DTSTransformStat_OK to reference the source fields, destination fields, and the Transform Data Task object, respectively. In the Transform Data Task Properties dialog box, click the Transformations tab and follow these steps:

  1. Select the Transformation arrow that goes from ProductID (source) to ProductID (destination) and click the Delete button.

  2. Click the New button. The Create New Transformation dialog box appears.

  3. Select ActiveX Script and click OK. The Transformation Options dialog opens.

  4. In the Name text box, enter Merge_ID_Name.

  5. Click the Source Columns tab. You need to tell the ActiveX transformation which fields are available. In the left pane, double-click ProductName. Now the field should be in the right pane. Click OK.

  6. On the General tab, click the Properties button.

  7. In the right pane of the dialog box, change the VBScript function Main() with the following code:

  8. Function Main()
     DTSDestination("ProductID") = DTSSource("ProductID") & "_" &
     Main = DTSTransformStat_OK
    End Function
  9. Click the Test button to check that you entered the code correctly.

  10. Click OK when the test is complete. Click Done and then OK to go back to the Transformations tab.

Converting the Unit Price

The customer's wants you to convert the unit price of the data from U.S. dollars to Euros. You can do this with an ActiveX transformation:

  1. Select the Transformation arrow that goes from UnitPrice (source) to UnitPrice (destination) and click the Delete button.

  2. Create a new ActiveX transformation with the name Euro_Conversion and change the VBScript function Main() with the following code:

  3. Function Main()
     DTSDestination("UnitPrice") = Round(DTSSource("UnitPrice") / 1.18119, 2)
     Main = DTSTransformStat_OK
    End Function
  4. Click the Test button to check that you entered the code correctly. Click Done and then OK until you get back to the DTS Designer editor.

  5. From the Package menu, select Save. For Package Name, enter Export Product Catalog; then click OK.

  6. Close the package.

  7. To execute the Export Product Catalog package, right-click the package and select Execute Package (see Figure 4).

Figure 4Figure 4 Package execution.

After the successful execution of the package, you'll be able to locate the file c:\catalog.txt and review its contents.

  • + Share This
  • 🔖 Save To Your Account