Lab 3: Transferring Data

10
Lab 3: Transferring Data Exercise 1: Using the Import/Export Wizard Exercise 2: Performing a Bulk Load Exercise 3: Creating an SSIS Solution Exercise 1: Using the Import/Export Wizard Scenario Legacy systems at Adventure Works require product data in comma-delimited text files. Also, a flat file containing information from a legacy system needs to be imported into a new SQL Server database. First, You must export data from the Person.Contact table in the AdventureWorks database to a text file, then import that data into a new database. Exercise Overview In this exercise you will use the Import and Export Wizard to import and export data from the SQL Server 2008 database. This exercise’s main tasks are: 1. Export data using the Import and Export Wizard. 2. Import data using the Import and Export Wizard. Task 1: Export data by using the Import and Export Wizard 1. In the Lab Launcher, next to 6231A-NY-SQL-01, click Launch. 2. Log on to NY-SQL-01 as Administrator using the password Pa$$w0rd. 3. Click Start, point to All Programs, point to Microsoft SQL Server 2008, and then click SQL Server Management Studio. 4. In the Connect to Server dialog box, click Connect. 5. In Object Explorer, expand Databases. 6. Right-click AdventureWorks2008, point to Tasks, and then click Export Data. 7. On the Welcome to SQL Server Import and Export Wizard page, click Next. 8. On the Choose a Data Source page, click Next.

Transcript of Lab 3: Transferring Data

Lab 3: Transferring Data

Exercise 1: Using the Import/Export Wizard

Exercise 2: Performing a Bulk Load

Exercise 3: Creating an SSIS Solution

Exercise 1: Using the Import/Export Wizard

Scenario

Legacy systems at Adventure Works require product data in comma-delimited text files.

Also, a flat file containing information from a legacy system needs to be imported into a

new SQL Server database. First, You must export data from the Person.Contact table in

the AdventureWorks database to a text file, then import that data into a new database.

Exercise Overview

In this exercise you will use the Import and Export Wizard to import and export data

from the SQL Server 2008 database.

This exercise’s main tasks are:

1. Export data using the Import and Export Wizard.

2. Import data using the Import and Export Wizard.

Task 1: Export data by using the Import and Export Wizard

1. In the Lab Launcher, next to 6231A-NY-SQL-01, click Launch.

2. Log on to NY-SQL-01 as Administrator using the password Pa$$w0rd.

3. Click Start, point to All Programs, point to Microsoft SQL Server 2008, and then click

SQL Server Management Studio.

4. In the Connect to Server dialog box, click Connect.

5. In Object Explorer, expand Databases.

6. Right-click AdventureWorks2008, point to Tasks, and then click Export Data.

7. On the Welcome to SQL Server Import and Export Wizard page, click Next.

8. On the Choose a Data Source page, click Next.

9. On the Choose a Destination page, specify the values in the following table, and then

click Next.

Property Value

Destination Flat File Destination

File name E:\Mod05\Labfiles\Contacts.txt

Locale English (United States)

Code page 1252 (ANSI - Latin I)

Format Delimited

Text qualifier <None>

Column names in the first data row Selected

10. On the Specify Table Copy or Query page, click Write a query to specify the data to

transfer, and then click Next.

11. On the Provide a Source Query page, type the following query, and then click Next.

SELECT Firstname, Lastname

FROM Person.Person

WHERE BusinessEntityID < 20

12. On the Configure Flat File Destination page, accept the default file settings, and then

click Next.

13. On the Save and Run Package page, ensure Run immediately is selected, and then

click Next.

14. On the Complete the Wizard page, click Finish.

15. On the The execution was successful page, verify that SSIS transferred 19 rows of data

to the file E:\Mod05\Labfiles\Contacts.txt, and then click Close.

16. Click Start, and then click Computer.

17. In Windows Explorer, browse to the E:\Mod05\Labfiles folder, and then double-click

the Contacts file.

18. After viewing the contacts, close Notepad.

19. Close Windows Explorer.

Task 2: Import Data Using the Import and Export Wizard

1. In Object Explorer, right-click AdventureWorks2008, point to Tasks, and then click

Import Data.

2. On the Welcome to SQL Server Import and Export Wizard page, click Next.

3. On the Choose a Data Source page, specify the values in the following table, and then

click Next twice.

Property Value

Property Value

Data source Flat File Source

File Name E:\Mod05\Labfiles\Contacts.txt

Locale English (United States)

Code Page 1252 (ANSI - Latin I)

Format Delimited

Text qualifier <none>

Header row delimiter {CR}/{LF}

Header rows to Skip 0

Column names in the first data row Selected

4. In the Choose a Destination page, accept the default values and click Next.

5. In the Select Source Table and Views page, accept the default values and click Next.

6. In the Save and Run Package page, click Next.

7. Click Finish.

8. Click Close.

Results: After completing this exercise, you should have successfully used the

Import and Export Wizard to import and export data.

Exercise 2: Performing a Bulk Load

Scenario

The Currency Type table needs to be imported into the Purchasing database.

Exercise Overview

In this exercise, the student will use bcp to export a table to a file, then use bulk insert to

import that data to another table.

This exercise’s main tasks are:

1. Export using bcp.

2. Create a new table for bulk import.

3. Use Bulk Insert to load data.

Task 1: Export using bcp.

1. Click Start, and then click Command Prompt.

2. In the Command Prompt window, type bcp AdventureWorks2008.Sales.Currency

out "E:\Mod05\Labfiles\Currency.dat" -T -c and press ENTER.

3. Close Command Prompt.

4. Click Start, and then click Computer.

5. Browse to E:\Mod05\Labfiles.

6. Confirm that Currency.dat is present.

7. Close Windows Explorer.

Task 2: Create a new table for bulk import.

1. In Object Explorer, expand AdventureWorks2008.

2. Right-click Tables, and then click New Table.

3. In the Properties pane, click the (Name) field and type CurrencyNew.

4. Click the Schema property and click Purchasing.

5. In the table, click the blank field under Column Name and type CurrencyCode.

6. Press TAB.

7. In the Column Properties pane, click the Length field and type 3.

8. In the table, click the blank field under Column Name and type Name.

9. Press TAB.

10. In the Column Properties pane, click the Data Type field, click nvarchar.

11. Click the Length field and type 50.

12. In the table, click the blank field under Column Name and type ModifiedDate.

13. Click the Data Type field, and then click datetime.

14. On the toolbar, click Save CurrencyNew.

15. In Object Explorer, right-click Tables and then click Refresh.

16. Expand Tables, confirm that the Purchasing.CurrencyNEW table is listed in Object

Explorer.

Task 3: Use Bulk Insert to load data.

1. In Object Explorer, right-click AdventureWorks2008 and then click New Query.

2. Type the following into the query window:

BULK INSERT AdventureWorks2008.Purchasing.CurrencyNEW

FROM ‘E:\Mod05\Labfiles\Currency.dat’

3. On the toolbar, click Execute.

4. In Object Explorer, right-click Purchasing.CurrencyNEW, and then click Select Top

1000 Rows.

5. Close Microsoft SQL Server Management Studio. Click Yes, then Save to save changes.

Results: After completing this exercise, you should have exported the Currency

table to a file named currency.dat then imported that data into a new table.

Exercise 3: Demonstration: Using the OPENROWSET Function

Demonstration Steps

Use the OPENROWSET Function to Import Data

1. Ensure that you have completed the previous demonstrations in this module.

2. In SQL Server Management Studio, in Object Explorer, right-click the dbo.SalesTaxRate table in the

Finance database, and click Select Top 1000 Rows.

3. View the query results, and verify that the dbo.SalesTaxRate table is currently empty.

4. Click New Query, and in the new query pane, enter the following Transact-SQL code:

INSERT INTO Finance.dbo.SalestaxRate

SELECT * FROM OPENROWSET (BULK 'D:\Demofiles\Mod06\SalesTaxRate.csv’,

FORMATFILE = 'D:\Demofiles\Mod06\TaxRateFmt.xml') AS rows;

5. Click Execute and note the number of rows affected.

Switch to the query pane that retrieves the top 1000 rows from the dbo.SalesTaxRate table and click

Execute to re-run the SELECT query. Note that the table is now populated with the same number of

rows

as you noted in the previous step.

Exercise 4: Creating an SSIS Solution

Scenario

For another legacy system, you must use SSIS create a text file that consists of the

ProductID and Name columns from the Production.Product table, and a column named

Price that is derived from the ListPrice column in the Production.Product table that

displays the price using the currency format defined by the locale of the computer.

The SSIS package must display a confirmation message when the data has been exported.

Exercise Overview

In this exercise, the student will create an SSIS package that can export a table to a text

file, transforming a column of data in the process.

This exercise’s main tasks are:

1. Creating an Integration Services project

2. Adding a data source and a connection manager

3. Adding a data flow task, an OLE DB source, a derived column, and a data

destination

4. Adding a script task

5. Testing the package

Task 1: Creating an SSIS Project

1. Click Start, point to All Programs, point to Microsoft SQL Server 2008, and then click

SQL Server Business Intelligence Development Studio.

2. In SQL Server Business Intelligence Development Studio, on the File menu, point to

New, and then click Project.

3. In the New Project dialog box, specify the values in the following table, and then click

OK.

Property Value or action

Templates Integration Services Project

Name ExportProducts

Location E:\Mod05\Labfiles\

Create directory for solution Cleared

4. If Solution Explorer is not visible, on the View menu, click Solution Explorer.

5. In Solution Explorer, right-click Package.dtsx, click Rename, type

ExportProductsToTextFile.dtsx, and then press ENTER.

6. In the Microsoft Visual Studio message box, click Yes to rename the package object.

Task 2: Add a data source and a connection manager

1. In Solution Explorer, right-click Data Sources, and then click New Data Source.

2. On the Welcome to the Data Source Wizard page, click Next.

3. On the Select how to define the connection page, click New.

4. In the Connection Manager dialog box, specify the values in the following table, and

then click OK.

Property Value

Provider Native OLE DB\SQL Server Native Client 10.0

Server name NY-SQL-01

Log on to the server Use Windows Authentication

Select or enter a database name AdventureWorks2008

5. On the Select how to define the connection page, click Next.

6. In the Completing the Wizard page, in the Data source name box, type Adventure

Works Managed Connection, and then click Finish.

7. Right-click anywhere inside the Connection Managers pane (at the bottom of the design

surface), and then click New Connection From Data Source.

8. In the Select Data Source dialog box, in the Available Data Sources list, click

Adventure Works Managed Connection, and then click OK.

Task 3: Add a data flow task, an OLE DB source, a derived column,

and a data destination

1. In the designer workspace, click the Control Flow tab.

2. On the View menu, click Toolbox.

3. In the Toolbox, drag the Data Flow Task object to the designer workspace.

4. Right-click the Data Flow Task object, click Rename, type Export Product Data, and

then press ENTER.

5. In the designer workspace, click the Data Flow tab.

6. In the Toolbox, in the Data Flow Sources section, drag the OLE DB Source object to the

designer workspace.

7. Click the OLE DB Source object, in the Properties pane, double-click the Name field,

type Products Data, and then press ENTER.

8. Right-click the Products Data object, and then click Edit.

9. In the OLE DB Source Editor dialog box, on the Connection Managers tab, specify the

values in the following table.

Property Value

OLE DB connection manager Adventure Works Managed Connection

Data access mode SQL Command

SQL command text SELECT ProductID, Name, ListPrice FROM

Production.Product

10. Click the Columns tab, verify that columns in the Available External Columns box are

mapped to columns with the same name in the Available Output Columns box, and

then click OK.

11. In the Toolbox, in the Data Flow Transformations section, drag the Derived Column

object to the designer workspace, below the Products Data object.

12. Click the Products Data object, and then drag the green connection onto the Derived

Column object.

13. Right-click the Derived Column object, and then click Edit.

14. In the Derived Column Transformation Editor dialog box, in the lower pane, specify

the values in the following table, and then click OK.

Property Value

Derived Column Name Price

Derived Column <add as new column>

Expression "$" + (DT_STR,10,1252)(DT_CY)ListPrice

Data Type Unicode string [DT_WSTR]

Length 11

15. In the Toolbox, in the Data Flow Destinations section, drag the Flat File Destination

object to the designer workspace, below the Derived Column object.

16. Click the Derived Column object, and then drag the green connection onto the Flat File

Destination object.

17. Right-click the Flat File Destination object, and then click Edit.

18. In the Flat File Destination Editor dialog box, click New.

19. In the Flat File Format dialog box, click Delimited, and then click OK.

20. In the Flat File Connection Manager Editor dialog box, in the File name box, type

E:\Mod05\Labfiles\Products.txt.

21. In the left pane, click the Advanced tab.

22. On the Advanced page, click the ListPrice column, click Delete, and then click OK.

23. In the Flat File Destination Editor dialog box, in the left pane, click Mappings, ensure

that that ListPrice column is not mapped to a destination column, and then click OK.

Task 4: Add a script task

1. In the designer workspace, click the Control Flow tab.

2. In the Toolbox, in the Control Flow Items section, drag a Script Task object onto the

designer workspace, below the Export Product Data object.

3. Right-click the Script Task object, and then click Edit.

4. In the Script Task Editor dialog box, click the General tab.

5. In the Name box, type MsgDone and then in the Description box, type Display

confirmation message.

6. In the left pane, click the Script page, and then click Edit Script.

- Note: Microsoft® Visual Studio® for Applications starts and generates a skeleton script.

7. In the Project Explorer tab, double-click ScriptMain.cs.

8. In the ScriptMain pane, scroll down to the Public Void Main() method, and then replace

the comment //TODO: Add your code here with the following code.

MessageBox.Show("Product data has been exported");

9. On the File menu, click Close Project.

10. On the File menu, click Exit.

11. In the Script Task Editor dialog box, click OK.

12. In the designer workspace, click the Export Product Data object, and then drag the

green connection onto the MsgDone script object.

Task 5: Test the package

1. On the Debug menu, click Start Debugging to run the project and transform and export

the data.

2. In the message box displaying the message Product data has been exported, click OK.

3. On the Debug menu, click Stop Debugging.

4. Click Start, and then click Computer.

5. In Windows Explorer, browse to the E:\Mod05\Labfiles\ folder, and then double-click

Products.

- Note: Notepad opens and displays the data in the file.

6. In Notepad, view the data, and then verify that it contains the product ID, the product

name, and the price formatted as expected.

- Note: The first several rows currently have a price of $0.

7. Close Notepad.

8. Turn off 6231A-NY-SQL-01 and delete changes.

Results: After completing this exercise, you should have created an SSIS Package

that will export a table and transform one column.