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

beginning microsofl sql server 2008 programming phần 9 ppt

73 272 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 73
Dung lượng 1,79 MB

Nội dung

We’re not going to go into real detail on this, because the options available will vary widely depending on the particular OLE DB data source you’ve selected. Indeed, the real point of the moment is just that: SSIS will alter the dialog to give you contextual choices that are relevant to the particular OLE DB data source you’re using. In the preceding example, I’ve simply clicked the “Column names in the first data row” option, and it’s time to click Next again. This takes us to choices for selecting what data we want out of our source database. We have two choices here: ❑ Copy: This has us do a straight copy out of a table or view. ❑ Query: This allows us to select out of pretty much any operation that’s going to yield a result set. Let’s start by taking a look at the Query option. If you choose this option and click Next, you get yet another dialog; this one pretty boring in the sense that all it does is give you a box where you can enter in the text of your query. (See Figure 18-5.) Figure 18-5 In my example here, I’ve shown where we could be setting up to export a list of employees that we hired in our first month of business. If you want to test this, you can type the query, and then try testing it with the Parse button to make sure that the query you want to run is syntactically valid, or you can copy it into a query window in the Management Studio and test it there. Then click Next to get to the dialog box shown in Figure 18-6. 547 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 547 Figure 18-6 For starters, this sets us up to decide what we want our flat file to look like. I can easily change the row delimiter. (For example, if you’re exporting for use in a Linux or UNIX based system, you may want to choose just a line feed rather than the line feed/carriage return option.) You can click Preview to see whether your query looks like it’s going to return the data that you expect. (See Figure 18-7.) Figure 18-7 Click OK, and you’re back to the Configure Flat File dialog. 548 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 548 Next, try clicking on the Edit Mappings… button, which takes you to the dialog box shown in Figure 18-8. Figure 18-8 At first, things will probably appear pretty mundane here, but, actually, things are getting a lot more interesting. Try clicking on any of the Destination column values. You’ll see a drop-down option appear, and that will include the option to ignore that column, essentially omitting a source column from the destination when you output the file. This should bring up the question of “Why would I have even put the column in the original query if I was just going to ignore it at output?” The reasons are multifold. First, you may have wanted the column there primarily for preview purposes to verify that the data is what you expected — for example, including the names to verify that it seems like the right people when you only intend to output the EmployeeID. Also, you may be using a query that is copied from some other source and want to avoid risking editing it (for example, if it is particularly complex). One other point: This dialog is shared with the direct table choice. (Back when we chose Query, we had the option of a table, remember?) So the option is even more applicable in direct table copy scenarios, where there may be many columns in the table that you don’t want in the end output. Next, try clicking on the Type column values, and you’ll see a number of choices. I highly recommend resizing the columns in this dialog so you can see what the Type column is really trying to show you. Just hover your mouse over the right side of the column header much as you would if you were using Excel, and then click and drag the column divider to the right to enlarge the column. 549 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 549 Most of the time you’ll stick with whatever the default conversion was, based on the source data type, but sometimes you may want to change the output type in some way, for example, treating integer data as a string to deal with some expectation of the end destination of your data. (Not all systems treat what is seemingly the same data the same way.) Finally, you can mess with the nullability and scale/precision of your data. It’s pretty rare that you would want to do this, and it’s something of an advanced concept, but suffice to say that if you were to use more advanced transformations (instead of the default Import/Export Wizard), then this might be an interest- ing option to facilitate error trapping of data that isn’t going to successfully go into your destination system. In most cases, however, you would want to do that with WHERE clauses in your original query. Now, let’s click Cancel to go back to our Configure Flat File dialog, and then click Next to move onto the next dialog. At this point, you’ll get a simple confirmation dialog. It will synopsize all the things that you’ve asked the wizard to do — in this case: ❑ Copy rows from [Query] to C:\Documents and Settings\xxx\My Documents\ TestExport.txt . ❑ The new target table will be created. ❑ The package will be saved to the package file C:\Documents and Settings\xxxx\ My Documents\Visual Studio 2005\Projects\Integration Services Project1\ Integration Services Project1\Package1.dtsx . ❑ The package will not be run immediately. Most of this is self-explanatory, but I want to stress a couple of things. First, it will create the file for us with the name specified; if, when we designed the package, the file had been detected as already existing, then we would have been given some options on whether to overwrite the existing file or just append our data onto the end of it. Perhaps more importantly, notice that a package is being created for you. This package is what amounts to an SSIS program. You can have SSIS execute that package over and over again (including scheduling it), and it will perform the defined export for you each time. Finally, note that it says that your package will not be run immediately; you need either to execute this package manually or schedule it to run. Click Next, and your package is created. Let me stress again that this is just something of a preparation step; your package has not run, and your data is not yet exported. You still need to execute or schedule the package to get your actual exported file. That side of things — the execution of the package — is the same for all SSIS packages though, and isn’t Import/Export Wizard-specific, so we’ll hold that side of things until the next section. 550 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 550 Examining Package Basics Fully examining all the parts of an SSIS package and the uses of each piece can fill up the entire book on its own. (Indeed, there are many books dedicated to the subject.) But understanding at least the funda- mentals is important to be able to make even the most basic use of Integration Services, so let’s take a look at the package that the Import/Export Wizard just created for us. Assuming you still have the Development Studio open (including the package we just created — if not, open that back up), you should have a fairly standard looking Visual Studio window up similar to Figure 18-9, with the exception of having some fairly Integration Services-oriented stuff in the main pane (as well as in the Connection Managers pane just below the main pane). Figure 18-9 Before we explore the Integration Services panes, let’s focus first on the standard Visual Studio Solutions Explorer pane (by default, in the upper right. It was what we right-clicked to start the Import/Export Wizard). You should see two packages there. The first — simply titled Package.dtsx — was a default 551 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 551 package created when we first chose the Integration Services template when we first created our new package. It should be empty, and can be deleted. Package1.dtsx will be the one you want to select (you can rename it if you choose) in order to follow the remaining discussion in this section. With that out of the way, let’s look at a few of the key panes… Connection Managers I know… I know…. You expected me to go top down! Well, besides my sadistic nature coming forward, I have a more logical reason to get the Connection Managers pane out of the way. First, it’s simple, and we’ll be a while on the main pane, so let’s get it out of the way. By this point, you may well already have a solid idea as to what the Connection Managers pane (fea- tured in Figure 18-10) is all about. It’s about allowing you quick access to all the connections that are used in our package. Figure 18-10 If you double-click on either of the two connection managers, you should see a dialog that will look pretty similar to the dialogs that you worked with earlier in the previous section. (Refer to Figures 18-3 and 18-4.) Notice, however, that they are not quite the same. Indeed, the FlatFileConnection Manager has a lot of the same information, but has a somewhat different editing format. For example, Figure 18-11 shows the part of the dialog oriented around editing the data types we want output. That said, there really isn’t anything new here. The connection managers area is just a path to allow us to edit our connection information (both for input and output) after we’ve exited the wizard. The Package Editor Pane This is something of the “meat and potatoes” section of the package. While we obviously couldn’t move much data around without the connections we discussed in the previous section, you’ll find that the con- nection side of things won’t look all that different from package to package. The actual flow and handler side of things will, however, often vary a great deal. As you can see along the top of Figure 18-12, the package editor has four tables to it, so let’s explore each of these in turn. Control Flow This does pretty much what its title would indicate. It controls flow between different logical constructs, or “tasks,” in our package. In our case, we have only one task defined, the Data Flow Task visible in Figure 18-12, but if you select that task (as I have in Figure 18-13), you’ll see that it will show an arrow coming out of it. 552 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 552 Figure 18-11 Figure 18-12 Figure 18-13 553 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 553 The arrow is representative of possible flow. We could, for example, add an FTP or external process task to our package to deal with elements of our Extract, Transform, and Load processes that can’t be handled within the one data flow task that we have. This control flow aspect is critical to many packages in sev- eral ways: ❑ The flow outlined in this part of the package establishes precedence. Think about this for a moment: If we need to FTP our data export to some external system after we’ve created it, then we need to be sure that the FTP process does not fire until the export portion of the package is complete; control of flow does this for us. ❑ If multiple tasks are available that do not have precedence on earlier tasks (or are dependent on the same task), the package can allow the tasks to branch and run simultaneously. ❑ Further branching can take place depending on specific return conditions from the previous task (for example, retrieving a file if our FTP succeeds, but notifying an operator if it doesn’t). What, exactly, there is to potentially edit, varies widely depending on the specific type of task you have. It might range from things as complex as connection and login information (as would be needed in something like the FTP task mentioned earlier) to something as simple as there being nothing to edit at all. (Indeed, were we to edit our data flow task, it would actually take us to the Data Flow tab, with nothing specific to control flow.) Data Flow Well, since our example is based around a data flow task, it’s probably not surprising to find a little bit more meat in this area. We can edit the data flow by either double-clicking our data flow task in the Con- trol Flow tab, or selecting the data flow tab, and then selecting the data flow you’re interested in from the drop-down in the top of the pane (as shown in Figure 18-14). Figure 18-14 Our wizard has populated these data flow items for us, but let’s take a quick look inside so we can relate to the various pieces; either double-click the Source – Query flow item, or right-click it and choose Edit. 554 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 554 This brings up the OLE DB Source Editor shown in Figure 18-15. The default option for editing is called the Connection Manager, but notice that, while there is overlap, there are several differences versus the dialog that we looked at earlier. If we were using a parameterized query, this would be where we could edit the properties of those parameters. Figure 18-15 Before we leave the Data Flow section, select the Error Output option for this dialog, as shown in Figure 18-16. The key thing to draw out of this dialog is that you have different options on how to handle data flow errors. For this simple wizard-generated data flow item, all errors will be considered fatal for the package, and the package will close with a fail return to whatever called the flow item. (In this case, the data flow task in the control flow area, which will in turn pass the failure on and cause the package to terminate.) Other options would include ignoring the error (and skipping the row) or redirecting the row (perhaps to a holding table where the row can be manually or programmatically repaired before reattempting to import). Consider these options as you graduate into more advanced packages. 555 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 555 Figure 18-16 Cancel out of all the dialogs to leave things as they were when we completed the wizard. Event Handlers Moving over to the Event Handlers tab, we can see the many events that we can supply code for. Acti- vate the drop-down (as I have in Figure 18-17). While the default is OnError, we can deal with a very wide range of scenarios. Let’s examine how a few of these might be used: ❑ OnError: Well, I would hope that this is the most obvious one. As you would expect, this event is fired in the event an error is detected. It allows you to address the error in some fashion. ❑ OnWarning: Basically the same as OnError, but it allows you to examine and possibly address warnings too. ❑ OnProgress: For long running processes, this can be handy for updating some form of progress indicator (perhaps a status table somewhere). ❑ OnPreExecute: Allows for things like preparation code to run prior to starting the main part of the task. And this is, of course, just a taste. What events to use will vary by need. 556 Chapter 18: Getting Integrated with Integration Services 57012c18.qxd:WroxBeg 11/22/08 10:41 AM Page 556 [...]... alerts, shown in Figure 19- 9 570 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 571 Chapter 19: Playing Administrator Figure 19- 8 Figure 19- 9 571 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 572 Chapter 19: Playing Administrator From here, we can select which alerts we want to make depending on what happens Choose Add and we get yet another rich dialog box, shown in Figure 19- 10 Figure 19- 10 Our first node —... forget that, if you’re going to be running scheduled tasks like this one, you need to have the SQL Server Agent running for them to be executed You can check the status of the SQL Server Agent by running the SQL Server Configuration Manager and selecting the SQL Server Agent service, or by navigating to the SQL Server Agent node of the Object Explorer in Management Studio Also, don’t forget to disable... command box; you can use this to import SQL scripts that you have saved in files 567 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 568 Chapter 19: Playing Administrator Figure 19- 4 Figure 19- 5 568 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 5 69 Chapter 19: Playing Administrator Let’s go ahead and move on to the Advanced tab for this dialog box, shown in Figure 19- 6; it’s here that we really start to see... you need to navigate to the SQL Server Agent node of the server for which you’re creating the operator Expand the SQL Server Agent node, right-click the Operators member, and choose New Operator Be aware that, depending on your particular installation, the SQL Server Agent Service may not start automatically by default If you run into any issues or if you notice the SQL Server Agent icon in Management... (Even among highly advanced SQL Server developers, programmatic construction of jobs and tasks is very rare indeed.) 566 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 567 Chapter 19: Playing Administrator Creating Jobs and Tasks Using Management Studio SQL Server Management Studio makes it very easy to create scheduled jobs Just navigate to the SQL Server Agent node of your server Then right-click the... have the Options node, shown in Figure 19- 12 572 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 573 Chapter 19: Playing Administrator Figure 19- 11 Figure 19- 12 573 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 574 Chapter 19: Playing Administrator Finally, we can go back to the Notifications node of the main New Job dialog box, shown in Figure 19- 13 Figure 19- 13 This window lets you bypass the older alerts model... whatever location you told your package to store it; open it up, take a look, and verify that it was what you expected Executing Using the SQL Server Agent We haven’t really discussed the SQL Server Agent up to this point, but, from an SSIS point of view, think of the SQL Server Agent as a job scheduler that allows you to do the same thing as running DTExec.exe, but doing so on a specific time and frequency... that are included in SQL Server Management Studio (we will touch very briefly on the new Policy Based Management) Be 57012c 19. qxd:WroxBeg 11/23/08 6:43 AM Page 564 Chapter 19: Playing Administrator aware, however, that there are more robust options available for the more advanced user that can allow you to programmatically control many of the administration features via the SQL Server Management Objects... utility run it for you on demand ❑ As a scheduled task using the SQL Server Agent: We’ll talk more about the SQL Server Agent in our next chapter, but for now, realize that executing an SSIS package is one of the many types of jobs that the agent understands You can specify a package name and time and frequency to run it in, and the SQL Server agent will take care of it ❑ From within a program: There... This option is really nice for mobility; you can easily save the package off and move it to another system ❑ SQL Server: This model stores the package in SQL Server Under this approach, your package will be backed up whenever you back up your MSDB database (which is a system database in every SQL Server installation) ❑ SSIS Package Store: This storage model provides the idea of an organized set of “folders” . SQL Server: This model stores the package in SQL Server. Under this approach, your package will be backed up whenever you back up your MSDB database (which is a system database in every SQL Server. expected. Executing Using the SQL Server Agent We haven’t really discussed the SQL Server Agent up to this point, but, from an SSIS point of view, think of the SQL Server Agent as a job scheduler. using Management Studio, you need to navigate to the SQL Server Agent node of the server for which you’re creating the operator. Expand the SQL Server Agent node, right-click the Operators member,

Ngày đăng: 09/08/2014, 14:21