1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Microsoft SQL Server 2000 Data Transformation Services- P5 pptx

50 512 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 402,69 KB

Nội dung

FIGURE 6.24 You can change your Transform Data tasks to the freestanding icon. DTS Connections and the Data Transformation Tasks P ART II 176 Whenever you create a new task using the Package Designer, a step, a task, and an icon for the step/task are all created at the same time. The Package Designer creates the icon that is appropriate for the particular custom task. After the point of creation, that icon is attached to the Step object and not the Task object. You can switch the task associated with the step to another task. (That’s what you’re doing when you change the Step’s TaskName property.) You can remove the Task object from the Package’s Tasks collection. The step will still be displayed in the Package Designer with the icon that was originally assigned to it. The connection between step and icon remains when saving and loading the package from any type of storage except Visual Basic code. When you SaveToVB, none of the visual representation of the package is saved. When you recreate the package by exe- cuting the saved code, the default visual representation of the package is recreated. An icon is assigned to each step based on the task that is associated with that step in the VB code. NOTE One more reminder. As far as I know, there is no Microsoft documentation regarding the freestanding Transform Data task icon or on any of the information I have pre- sented in this section. CAUTION 09 0672320118 CH06 11/13/00 4:56 PM Page 176 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Conclusion There’s a lot to learn about the Transform Data task! This has been a long chapter, but there’s still a lot more to learn about this task. The next chapter, “Writing ActiveX Scripts for a Transform Data Task,” shows you how to implement precise programmatic control in the row-by-row processing of your data. The Transform Data Task C HAPTER 6 6 T HE T RANSFORM D ATA TASK 177 09 0672320118 CH06 11/13/00 4:56 PM Page 177 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 09 0672320118 CH06 11/13/00 4:56 PM Page 178 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHAPTER 7 Writing ActiveX Scripts for a Transform Data Task IN THIS CHAPTER • When You Should Use the ActiveX Script Transformation 180 •Transformation ActiveX Scripts Basics 182 • The Transformation ActiveX Script Development Environment 183 • Choosing a Scripting Language 187 • Setting the DTS Transformation Status 188 •Creating and Using Local Variables 192 •Creating and Using Global Variables 194 •Creating and Using Lookups 198 • Using ActiveX Scripts or Modifying the Source Query 202 • Separating Information from One Record into Several Records 206 • Combining Information from Several Records into One 210 10 0672320118 CH07 11/13/00 4:59 PM Page 179 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. DTS Connections and the Data Transformation Tasks P ART II 180 Data transformation scripts give DTS its flexibility and versatility. These scripts allow you to manipulate the data in each field of every row. All the code in the transformation script is exe- cuted once for each record in the data source. Other tasks, like the Bulk Insert, and other trans- formations, like the Copy Column, certainly move data faster, but they can only be used in specific situations. The ActiveX Script transformation can be used in almost every data trans- formation situation—and it’s usually fast enough. The needs of data transformation can be very complex. You may need to transform a field in different ways depending on a number of specific circumstances. You can accomplish a lot of detailed data manipulation with SQL queries, but there are times when programmatic require- ments overwhelm the set-based logic of SQL. The ActiveX transformation in the Transform Data task is a Rapid Application Development (RAD) tool because it lets you use the complex logic you need to apply to your data while still achieving excellent performance. Before DTS was included with SQL 7.0, I used Transact-SQL cursors in stored proce- dures to do what I now accomplish with transformation scripts. I know some data- base developers who would never use a Transact-SQL cursor because of its poor performance. I also know some developers who have been reluctant to try script transformations in DTS because the processing of these scripts seems to be very simi- lar to the operation of a cursor. An ActiveX Data Transformation script is quicker than a Transact-SQL cursor—a lot quicker. It’s optimized for high-speed data movement. Yes, you can slow it down by writing complex code. But if you need complexity in your data transformations, trans- formation scripts are a great place to implement that complexity. NOTE You can learn more about writing ActiveX scripts in Chapter 16, “Writing Scripts for an ActiveX Script Task.” You can learn about debugging scripts in Chapter 27, “Handling Errors in a Package and Its Transformations.” When You Should Use the ActiveX Script Transformation The basic rule of an ActiveX Script transformation is to use it when nothing else is going to work: • If you can use a Bulk Insert or some other task, consider using them first. • If you can use one of the other transformations, use them. 10 0672320118 CH07 11/13/00 4:59 PM Page 180 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. • If your transformation logic is too complex for anything else, use the ActiveX Script Transformation. • If you could do the transformation some other way, but it would take too long and it would be too hard to work out the logic, use the ActiveX Script to get the job done on time. Deciding Between One Task and Many Sometimes it’s possible to meet the data manipulation requirements in a couple of different ways: •Write a transformation script in a Transform Data task. •Create a Bulk Insert task followed by a couple of Execute SQL tasks. This way, you would get the data into SQL Server from a text file in the fastest possible way. You would use the rapid set-oriented processing of SQL to finish the detailed data manipulation—updating rows, deleting rows, and moving records to other tables. Which strategy results in the quickest development time? Which one gives the best performance? Which one will be easier to maintain as additional transformation needs are discovered? I can usually create a Transform Data task with a transformation script faster than setting up a Bulk Insert task and a couple of Execute SQL tasks. I can often achieve better performance by using the Bulk Insert with a couple of Execute SQL tasks. I usually find that a Transform Data task is a more maintainable solution because an additional change can be added in the pro- grammatic logic where it is needed. In general, transformation scripts become a better solution as the complexity of your data manipulation logic increases. Using the Variety of Transformation Types You didn’t have much choice regarding transformation types in SQL Server 7.0. If you weren’t doing a straight copy of fields from source to destination, you had to use an ActiveX Script transformation. In SQL Server 2000, you can choose from the nine different transformation types. Basic date and string manipulation that would have required an ActiveX Script in the past can now be accomplished with another transformation type. Use a specific transformation type to accomplish a specific job whenever you can. In fact, if you have a particular kind of transformation that you use frequently, the best way to improve its performance is to make it into a Custom Transformation. See Chapter 32, “Creating a Custom Transformation with VC++.” Writing ActiveX Scripts for a Transform Data Task C HAPTER 7 7 W RITING A CTIVE X S CRIPTS 181 10 0672320118 CH07 11/13/00 4:59 PM Page 181 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Transformation ActiveX Scripts Basics The code in an ActiveX Script transformation is run repeatedly as a Transform Data task is executed. Here is the logical sequence of events: 1. The package executes the Transform Data task. 2. The Transform Data task (the data pump) runs the source query. 3. The values for the first record in the recordset returned by the source query are loaded into the transformation’s collection of source columns. 4. The data pump executes the transformations in the order of their ordinal numbers in the Transformations collection. Each transformation can use the information from one or more of the source columns and may assign values to one or more of the destination columns. 5. Each script used in an ActiveX Script transformation must have an entry function. The default name for the entry function is Main. When the ActiveX Script transformation is executed, the data pump calls this entry function. 6. The code in the entry function is executed. Other functions in the script may be called. Any of the functions in the script may assign values to destination columns. The script can use information from the source columns, lookups defined for the task, and global variables defined for the package. 7. The entry function must return a transformation status code to the data pump. You can use this status code to insert a record, skip inserting a record, skip fetching a new record, return information, and/or return an error. 8a. If the last transformation executed for a record returns the transformation status code DTSTransformStat_OK, the values in the transformation’s destination columns are loaded into the data destination. If you are not using Fast Load, the record is inserted DTS Connections and the Data Transformation Tasks P ART II 182 When I created a Transform Data task in SQL Server 7.0, I often put all the columns from the source and the destination in one ActiveX Script transformation. I’m moving away from that strategy in SQL Server 2000. I like using the new types of transforma- tions, especially the one that transforms dates. My new strategy is to use one of each of the appropriate transformation types, divid- ing the columns into the appropriate types of transformations. With the date trans- formation type, I create one transformation for each combination of source and destination date formats that I’m using. TIP 10 0672320118 CH07 11/13/00 4:59 PM Page 182 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. individually into the destination. If you are using Fast Load, the record is saved for load- ing later as a part of a batch. The values of the destination columns are set to null. The values for the next record from the data source are loaded into the transformation’s source columns. 8b. If the last transformation executed for a record returns the transformation status code DTSTransformStat_SkipFetch, the processing is the same as for DTSTransformStat_OK, except that the values for the transformation’s source columns are left unchanged. 8c. If the last transformation executed for a record returns the transformation status code DTSTransformStat_SkipInsert, a record is not inserted into the destination. The values for the next record from the data source are loaded into the transformation’s source columns. The values in the transformation’s destination columns are not set to null. They keep their values as the processing starts for the next source record. 9. Steps 3 through 7 are repeated for all the records returned by the source query. 10. If you are using the fast load option, the data pump loads the records into the data desti- nation when the number of destination records specified by the InsertCommitSize prop- erty has been reached. Writing ActiveX Scripts for a Transform Data Task C HAPTER 7 7 W RITING A CTIVE X S CRIPTS 183 You can reference the same destination column in two or more transformations. If you do this, you will receive a warning message from the DTS Designer: “Same destination column ‘au_id’ exists in two transformations, which may cause a potential problem. Do you still want to continue?” The potential problem is that if you assign a destination column twice, the second assignment will overwrite the first. This could present a confusing debugging situa- tion. But there is also a potential benefit in doing this. If you add a transformation column that has already been assigned a value to an ActiveX Script transformation, you can use the assigned value in your programmatic logic. NOTE The Transformation ActiveX Script Development Environment Figure 7.1 shows the ActiveX Script Transformation Properties dialog, which opens when you create a new ActiveX transformation. You can also open the dialog by double-clicking the transformation’s mapping line. 10 0672320118 CH07 11/13/00 4:59 PM Page 183 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. FIGURE 7.1 The ActiveX Script Transformation Properties dialog provides a simple user interface for creating transformation scripts. When you first open the ActiveX Script Transformation Properties dialog, you see a default script that has been generated already. This default script gives you the same transformation result as a Copy Column transformation. Each field in the source is copied to the same field in the destination, based on the ordinal position of the fields in the two collections. The first field is copied to the first field in the destination, the second field is copied to the second field in the destination, and so on. The names of the fields are ignored in this mapping process. Listing 7.1 is an example of a default script. The data source is the authors table from the pubs sample database. The data destination has fields with identical names as the source, except that the first field, au_id, is not included. The first eight fields in the source column collection have been mapped to the eight fields in the destination column collection. LISTING 7.1 Sample Script Mapping Fields from a Source to a Destination ‘************************************************************************ ‘ Visual Basic Transformation Script ‘ Copy each source column to the ‘ destination column ‘************************************************************************ Function Main() DTSDestination(“au_lname”) = DTSSource(“au_id”) DTS Connections and the Data Transformation Tasks P ART II 184 10 0672320118 CH07 11/13/00 4:59 PM Page 184 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. DTSDestination(“au_fname”) = DTSSource(“au_lname”) DTSDestination(“phone”) = DTSSource(“au_fname”) DTSDestination(“address”) = DTSSource(“phone”) DTSDestination(“city”) = DTSSource(“address”) DTSDestination(“state”) = DTSSource(“city”) DTSDestination(“zip”) = DTSSource(“state”) DTSDestination(“contract”) = DTSSource(“zip”) Main = DTSTransformStat_OK End Function The default script is very useful when the fields have been lined up in the proper order. In a sit- uation like this, however, it’s not very helpful. The dialog provides three ways to modify or create a transformation script: • Automatically generate the script. The default script is generated when the ActiveX transformation is first created. If you want, you can regenerate the script in a different scripting language. You may also want to return to the original script after experimenting with some changes. You can re-create the default script by clicking the Auto Gen. button. •Insert a script from a file. A Browse button is provided so you can choose the file. •Write the script in the Script textbox. The tabs on the left side of the ActiveX Script Transformation Properties dialog provide assis- tance in writing and editing the script: • The first tab has a list box for choosing the scripting language. Prototypes of all the functions in the language you have chosen are available in the second list box. If you double-click on any of the functions, the prototype is copied into the text of your script at the point where you have placed the cursor. This tab also has a text box for choosing the entry function for your script. • The second tab, shown in Figure 7.2, has a Package Object Browser. Source columns, destination columns, lookups, global variables, task constants, and step constants are all available for selection and insertion into your script. • If you have enabled the Multiphase Option, you will have a third tab where you can name the entry function for each of the phases. The use of multiple phases is discussed in Chapter 9, “The Multiphase Data Pump.” Writing ActiveX Scripts for a Transform Data Task C HAPTER 7 7 W RITING A CTIVE X S CRIPTS 185 LISTING 7.1 Continued 10 0672320118 CH07 11/13/00 4:59 PM Page 185 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... if this transformation status is received for the last transformation in the Transformations collection • The data pump continues processing with the next record from the data source if this transformation status is received for the last transformation in the Transformations collection DTSTransformStat_SkipRow Skip all transformations for this row • Value 2 • There are no error messages • The data pump... test that can be run from the Transformation tab of the Data Transformation Properties dialog The results of the script test are displayed on the screen and saved to a text file You can find many errors by testing that you can’t find by parsing • The Save button, a new addition to SQL Server 2000, saves the script to a vbs or bas file • The Undo button, also new in SQL Server 2000, lets you undo your recent... in the source • Value 4 • The data pump continues on with the next transformation for this record • The data pump inserts the record into the data destination, and the values of all the destination columns are set to null if this transformation status is received for the last transformation in the Transformations collection • The data pump stays on the same record in the data source and begins processing... received for the last ActiveX Script transformation in the Transformations collection Values that have been assigned to destination columns are not set to null • The data pump continues processing with the next record from the data source if this transformation status is received for the last transformation in the Transformations collection DTSTransformStat_DestDataNotSet This transformation status is used... Scripts for a Transform Data Task CHAPTER 7 201 Using a Lookup to Modify Data One of the new features in SQL Server 2000 is the ability to use data modification queries in lookups The ability to call inserts, updates, deletes, and stored procedures with lookups gives the Transform Data task functionality that is similar to the Data Driven Query task You can create a Transform Data task that inserts... are four additional Transformation Status values that can be used with data- driven queries Those values are discussed in Chapter 8, “The Data Driven Query Task.” DTSTransformStat_OK The transformation script was successful • Value 1 • There are no error messages • The data pump continues on with the next transformation for this record • The data pump inserts the record into the data destination, and... pick its datatype from a list When you create a global variable in code, you cannot specify its datatype explicitly The datatype will be assigned automatically depending on the type of data that you assign to the variable The datatype of the global variable will be changed automatically when you assign data of another datatype to the global variable: • Create a global variable with a string datatype:... were a new record, if this transformation status is received for the last transformation in the Transformations collection DTSTransformStat_SkipInsert Skip the insert for this record • Value 8 • There are no error messages • The data pump continues on with the next transformation for this record • The data pump skips inserting the record into the data destination if this transformation status is received... tables, you could have a very slow data transformation It would usually be faster to execute two separate Transform Data tasks, one to load each table, rather than executing one Transform Data task that uses an Insert lookup query to load a table Also consider the possibility of using a Data Driven Query task or a Parallel Data Pump task 202 DTS Connections and the Data Transformation Tasks PART II Using... a Data Driven Query, of course—at least, you could divide one record into four tables Now you can also do it with a Transform Data task by using Insert lookup queries to fill as many different tables as you want The problem with this strategy is that you lose the high-performance data manipulation that you normally have available in the Transform Data task Using Fast Load as you move data into SQL Server, . button, a new addition to SQL Server 2000, saves the script to a .vbs or .bas file. • The Undo button, also new in SQL Server 2000, lets you undo your. Browser. DTS Connections and the Data Transformation Tasks P ART II 186 The Package Object Browser is a great addition to SQL Server 2000 because you don’t need

Ngày đăng: 26/01/2014, 15:20

w