module 8 importing and exporting data

25
Module 8 Importing and Exporting Data

Upload: abra-ramirez

Post on 03-Jan-2016

76 views

Category:

Documents


3 download

DESCRIPTION

Module 8 Importing and Exporting Data. Module Overview. Transferring Data To/From SQL Server Importing & Exporting Table Data Inserting Data in Bulk. Lesson 1: Transferring Data To/From SQL Server. Overview of Data Transfer Available Tools for Data Transfer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Module 8 Importing and Exporting Data

Module 8

Importing and Exporting Data

Page 2: Module 8 Importing and Exporting Data

Module Overview

• Transferring Data To/From SQL Server

• Importing & Exporting Table Data

• Inserting Data in Bulk

Page 3: Module 8 Importing and Exporting Data

Lesson 1: Transferring Data To/From SQL Server

• Overview of Data Transfer

• Available Tools for Data Transfer

• Improving the Performance of Data Transfers

• Disabling & Rebuilding Indexes

• Disabling & Enabling Constraints

• Demonstration 1A: Disabling & Enabling Constraints

Page 4: Module 8 Importing and Exporting Data

Scenarios

• Copying or moving between servers

• Exporting query data to a file

• Importing table data from a file

• Copying to or from a view

• Transforming collations

Overview of Data Transfer

ETL:

Extract Transform Load

Page 5: Module 8 Importing and Exporting Data

Available Tools for Data Transfer

Bulk Copy Program (bcp)

BULK INSERT

C:\> bcp

XML Bulk Load < >

Import / Export Wizard

OPENROWSET(BULK)

Page 6: Module 8 Importing and Exporting Data

Improving the Performance of Data Transfers

• Disable constraints, indexes, and triggers No need to check constraints as each row is loaded

Indexes don’t need to be maintained during import

Important to check business requirements before disabling triggers

• Minimizing locking Consider the use of TABLOCK to speed up the import

• Minimizing logging Database must be in BULK_LOGGED or SIMPLE model

Additional requirements on table structure and locking

• Minimize data conversions Use native format when transferring data between SQL Servers

Page 7: Module 8 Importing and Exporting Data

Disabling & Rebuilding Indexes

• Disabling an index Prevents user access to the index

Prevents access to the data if it is a clustered index

Keeps index definition in metadata

Speeds up data import in tables, as the index is not maintained during the import

• Enabling an index Rebuilds the index entirely

Is easy to automate as the metadata is still present

• Enabling and disabling indexes can be used as an alternative to dropping and recreating indexes

Page 8: Module 8 Importing and Exporting Data

Disabling & Enabling Constraints

• Disabling PRIMARY KEY and UNIQUE constraints Is achieved by disabling the associated index

Causes associated indexes to be rebuilt when enabled

Can cause failures during re-enabling if duplicate values exist

Also causes associated foreign key constraints to be disabled

• Disabling FOREIGN KEY and CHECK constraints Can be performed directly on the constraint

Causes existing data to not be verified when re-enabled

• When you enable a FOREIGN KEY or CHECK constraint, existing data is not verified by default

Page 9: Module 8 Importing and Exporting Data

Demonstration 1A: Disabling & Enabling Constraints

• In this demonstration, you will see how to disable and re-enable a Check constraint

Page 10: Module 8 Importing and Exporting Data

Lesson 2: Importing & Exporting Table Data

• Overview of SQL Server Integration Services

• Demonstration 2A: Working with SSIS

• SQL Server Import/Export Wizard

• Demonstration 2B: Using the Import/Export Wizard

Page 11: Module 8 Importing and Exporting Data

Overview of SQL Server Integration Services

• SSIS is a rich framework to develop ETL solutions

• SSIS packages contain Data Sources and Destinations

Control and Data Flows

Transformations to be performed

• SSIS packages can be run using dtsrun command line utility

SQL Server Agent jobs

• SSIS packages developed using SQL Server Business Intelligence

Development Studio (BIDS)

Import / Export Wizard

Page 12: Module 8 Importing and Exporting Data

Demonstration 2A: Working with SSIS

In this demonstration you will see how to use SQL Server Integration Services to export data from a table to a flat file

Page 13: Module 8 Importing and Exporting Data

SQL Server Import/Export Wizard

Easy to use wizard for creating an SSIS package that performs simple data transfers

Page 14: Module 8 Importing and Exporting Data

Demonstration 2B: Using the Import/Export Wizard

In this demonstration you will see how to use the Import/Export Wizard to export data to a CSV file

Page 15: Module 8 Importing and Exporting Data

Lesson 3: Inserting Data in Bulk

• bcp Utility

• Demonstration 3A: Working with bcp

• BULK INSERT Statement

• Demonstration 3B: Working with BULK INSERT

• OPENROWSET Function

• Demonstration 3C: Working with OPENROWSET

Page 16: Module 8 Importing and Exporting Data

BCP Utility

• Is a command line tool to import and export data

• Uses a format file when transferring between SQL Instances

Creating a format file:

bcp Adv.Sales.Currency format nul -T -c –x -f Cur.xml

Exporting data into a file:

bcp Adv.Sales.Currency out Cur.dat -T –c

Importing data using a format file:

bcp tempdb.Sales.Currency2 in Cur.dat -T -f Cur.xml

Creating a format file:

bcp Adv.Sales.Currency format nul -T -c –x -f Cur.xml

Exporting data into a file:

bcp Adv.Sales.Currency out Cur.dat -T –c

Importing data using a format file:

bcp tempdb.Sales.Currency2 in Cur.dat -T -f Cur.xml

Page 17: Module 8 Importing and Exporting Data

Demonstration 3A: Working with bcp

• In this demonstration, you will see how to import a file using the bcp utility

Page 18: Module 8 Importing and Exporting Data

BULK INSERT Statement

• Provides options similar to bcp

• Runs in the SQL Server process

• Has CHECK_CONSTRAINTS and FIRE_TRIGGERS options

• Can be executed within a user-defined transaction

BULK INSERT AdventureWorks.Sales.OrderDetail FROM 'f:\orders\neworders.txt' WITH ( FIELDTERMINATOR =' |', ROWTERMINATOR =' |\n' );GO

BULK INSERT AdventureWorks.Sales.OrderDetail FROM 'f:\orders\neworders.txt' WITH ( FIELDTERMINATOR =' |', ROWTERMINATOR =' |\n' );GO

Page 19: Module 8 Importing and Exporting Data

Demonstration 3B: Working with BULK INSERT

• In this demonstration, you will see how to import a file using the BULK INSERT command

Page 20: Module 8 Importing and Exporting Data

OPENROWSET Function

• Allows access to remote data by connecting to a remote data source using an OLE-DB provider

• Offers a built-in BULK provider for bulk imports from files Can be used in a FROM clause with full functionality Can use INSERT .. SELECT to insert data Offers unique table hints to control operation

SELECT * FROM OPENROWSET( BULK 'c:\mssql\export.csv',

FORMATFILE = 'c:\mssql\format.fmt',FIRSTROW = 2) AS a;

GO

SELECT * FROM OPENROWSET( BULK 'c:\mssql\export.csv',

FORMATFILE = 'c:\mssql\format.fmt',FIRSTROW = 2) AS a;

GO

Page 21: Module 8 Importing and Exporting Data

Demonstration 3C: Working with OPENROWSET

• In this demonstration, you will see how to import a file using OPENROWSET

Page 22: Module 8 Importing and Exporting Data

Lab 8: Importing and Exporting Data

• Exercise 1: Import the Excel spreadsheet

• Exercise 2: Import the CSV file

• Exercise 3: Create and test an extraction package

• Challenge Exercise 4: Compare loading performance (Only if time permits)

Logon information

Estimated time: 45 minutes

Virtual machine 623XB-MIA-SQL

User name AdventureWorks\Administrator

Password Pa$$w0rd

Page 23: Module 8 Importing and Exporting Data

Lab Scenario

Proseware regularly receives updates of currencies and exchange rates from an external provider. One of these files is provided as an Excel spreadsheet, the other file is provided as a comma-delimited text file. You need to import both these files into tables that will be used by the Direct Marketing team within Proseware.

Periodically the Marketing team requires a list of prospects that have not been contacted within the last month. You need to create and test a package that will extract this information to a file for them.

You are concerned about the import performance for the exchange rate file and you are considering disabling constraints and indexes on the exchange rate table during the import process. If you have time, you will test the difference in import performance.

Page 24: Module 8 Importing and Exporting Data

Lab Review

• What kind of information needs to be present in a format file?

• Which tool or command should you use to read an entire XML document into a column in a SQL Server table?

Page 25: Module 8 Importing and Exporting Data

Module Review and Takeaways

• Review Questions

• Best Practices