In the real world, things are not always as easy as expected. Try to migrate an application based on an Access database or on .dbf files created with dBase or Fox and you will see how difficult it can be. Occasionally, you may need to obtain information from other sources, such as mainframes or other database or legacy systems. You may also need to create a new application based on an existing one and enhance the database while users continue to use the legacy application during your development process.
These three examples demonstrate the need to understand the features of Integration Services. You will initially address Integration Services by creating a simple export package using a wizard.
In SQL Server Management Studio, right-click the AdventureWorks database in Object Explorer and select Tasks Export Data. This will start the SQL Server Import And Export Data Wizard.
After the welcome page appears, your server and database will already be selected in the Choose A Data Source page.
On the Choose A Destination page, select Flat File Destination from the Destination drop-down list and enter a filename.
Check the Column Names In The First Data Row checkbox to add column names at the beginning of the text file.
On the Specify Table Copy Or Query page, keep the Copy Data From One Or More Tables Or Views option selected.
On the Configure Flat File Destination page, select [AdventureWorks].[Sales].[CurrencyRate].
On the Save And Execute Package page, uncheck the Execute Immediately checkbox. Check the Save SSIS Package option and select the File System option.
In the Package Protection Level dialog box, select Do Not Save Sensitive Data from the drop-down list.
Finally, in the Save SSIS Package dialog box, assign a name and filename to your package.
Open Visual Studio 2005.
On the File menu, point to Open and then click File
Navigate to your package file (with the .dtsx extension) and open it. You will see something similar to the following figure.
The Package editor displays four tabs.
The Data Flow tab displays the exporting process from a database connection to a flat file.
Right-click each element, the connections, and the green arrow to view how the package stores all of the information needed to execute the task. Moreover, move your mouse pointer over the Toolbox tab to view a variety of tasks and elements that are available to use in a package.
Consider the following scenario: You need to update the CurrencyRates table with new information provided in an Excel worksheet. The requirements state that you must assign the datetime fields with the dates of the insert process. To accomplish this, you must intercept the import process to assign the date and time.
Open Visual Studio 2005.
From the File menu, select New Project, then select the Business Intelligence Projects type and the Integration Services Project template.
Enter a name for your project. When you click OK in the dialog box, you will view the same environment that is seen when you open the package created by the wizard (as in Step 3 in the previous procedure).
Select the Data Flow tab. From the Data Flow Sources section in the Toolbox, drag an Excel Source to the designer.
Right-click it (at the bottom of the designer) and select Edit.
Click the New button to the right of the OLE DB Connection Manager drop-down list.
Browse to the \Ch10\SSIS\CurrencyRate200602.xls file in the sample files, click the Open button, and then click OK.
From the Name Of The Excel Sheet drop-down list, select CurrencyRate$ and click OK. This completes the source configuration.
From the Data Flow Destinations section in the Toolbox, drag a SQL Server Destination.
Right-click it and select Edit. You will receive a message that there is no connection.
To the right of the OLE DB Connection Manager drop-down list, click the New button and define the connection to AdventureWorks in your database.
Select [Sales].[CurrencyRate] from the Use A Table Or View drop-down list and click Cancel. You must follow additional steps to complete the definition of this destination, which you will perform later.
From the Data Flow Transformations section in the Toolbox, drag a Script Component. When you do this, a dialog box will appear.
Select Transformation and click OK.
Right-click the Script Component and select Add Path.
In the dialog box, select Excel Source in the From drop-down list and Script Component in the To drop-down list. When you click the OK button, a new dialog box appears. Select Excel Source Output from the Output drop-down list and click OK.
Right-click and again select Add Path on the Script Component. Choose SQL Server Destination from the To drop-down list.
Right-click the Script Component and select Edit.
In the Input Columns step, check the following fields:
Click the Inputs And Outputs step, expand the Output node, click the Output Columns folder, and then click the Add Column button.
Enter ThisDate as the name of the column, and select Date [DT_DATE] as the datatype.
Using the same datatype, add another column with ThisDate2 as the name. (You need one output column for each destination column.)
Add two additional columns named AvgRate and EODRate , and select Currency [DT_CY] as the datatype.
In the Script step, click the Design Script button.
Add the following code in the Input0_ProcessInputRow method. You can find the code at \Ch10\SSIS\ScriptMain.vb in the sample files.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) 'Get the date and time from the system Dim ActualDate As Date = System.DateTime.Now 'Assign the same value to both columns With Row .ThisDate = ActualDate .ThisDate2 = ActualDate 'Convert types to currency destination .AvgRate = CDec(.AverageRate) .EODRate = CDec(.EndOfDayRate) End With End Sub
Close the Script Editor and click OK in the Edit Script Component dialog box.
Right-click the SQL Server Destination and select Edit.
Select [Sales].[CurrencyRate] from the Use A Table Or View drop-down list.
In the Mapping step, assign <ignore> to the CurrencyRateID column (it is an identity column). Assign ThisDate to CurrencyRateDate and ThisDate2 to ModifiedDate. Assign AvgRate to AverageRate and EODRate to EndOfDayRate.
Save the package.
You will see an alert icon in the SQL Server Destination component. This alert appears because the Script Component manages a string without any length control. Since the destination field has a specific size for the From and To Currency codes, the alert informs you that some data may be truncated.