Module 15
Importing and Exporting Data
Module Overview
• Transferring Data to and from SQL Server
• Importing and Exporting Table Data
• Using bcp and BULK INSERT to Import Data
• Deploying and Upgrading Data-Tier Applications
Lesson 1: Transferring Data to and from SQL
Server
• Overview of Data Transfer
• Data Transfer Tools
• Improving the Performance of Data Transfer
• Disabling and Rebuilding Indexes
• Disabling and Enabling Constraints
• Demonstration: Disabling and Enabling
Constraints
• Partition Switching
• Demonstration: Switching Partitions for Data
Transfer
Overview of Data Transfer
• ETL:
• Extract
• Transform
• Load
• Scenarios:
• Copying or moving data between servers
• Exporting query data to a file
• Importing file data to a table
• Transforming or restructuring data
Data Transfer Tools
• SQL Server Integration Services
• Import and Export Wizard
• bcp (bulk copy program)
• BULK INSERT
• OPENROWSET (BULK)
Improving the Performance of Data Transfer
• Disable constraint, indexes, and triggers:
• No need to check constraints as each row is loaded
• Indexes don’t have to be maintained during import
• Check business requirements before disabling triggers
• Minimize locking:
• Consider use of TABLOCK to speed up import
• Minimize logging:
• Database must be in BULK_LOGGED or SIMPLE
recovery model
• Requirements for minimal logging must be met
Disabling and Rebuilding Indexes
• Disabling an index:
• Prevents user access to the index
• Prevents access to the data for a clustered index
• Keeps index definition in metadata
• Enabling an index:
• Rebuilds the index entirely
• Is easy to automate because the definition is available
from metadata
• Disabling and enabling indexes is an alternative
to dropping and recreating indexes for bulk
imports
Disabling and Enabling Constraints
• Disabling PRIMARY KEY and UNIQUE constraints:
• Achieved by disabling the associated index
• Causes associated indexes to be rebuilt when enabled
• Can cause failures when re-enabled if data that violates
the constraint exists
• Causes associated foreign key constraints to be
disabled
• Disabling FOREIGN KEY and CHECK constraints:
• Performed directly on the constraint
• Constraint must be enabled WITH CHECK to verify
existing data, otherwise it is untrusted
Demonstration: Disabling and Enabling
Constraints
In this demonstration, you will see how to:
• Disable and enable primary key constraints
• Disable and enable unique constraints
• Disable and enable foreign key constraints
• Disable and enable unique constraints
Partition Switching
Table 1 Table 2
Partition Partition
• Partition switching:
• Tables must be in the same filegroup
• Schema must match
• Target partition must be empty
• Typically used to manipulate partitions in a partition
table
• Partition switching with unpartitioned tables:
• It’s not required that one of the tables is partitioned
• Works on any edition of SQL Server
Demonstration: Switching Partitions for Data
Transfer
In this demonstration, you will see how to use
partition switching to:
• Load data into a partitioned table
• Load data into an unpartitioned table
Lesson 2: Importing and Exporting Table Data
• Linked Servers
• Overview of SQL Server Integration Services
• Demonstration: Working with SSIS
• The SQL Server Import and Export Wizard
• Demonstration: Using the SQL Server Import and
Export Wizard
Linked Servers
• Execute commands against remote data sources
• Managing Linked Servers:
• From SSMS or using Transact-SQL
• Define a data source and a security context for the
connection
• Querying Linked Servers:
• Four-part name:
• server.database.schema.table
• OPENQUERY:
• Pass-through query—must be in linked server’s query
language
• If the provider supports it, you can run DML operations against
results returned by OPENQUERY
Overview of SQL Server Integration Services
• The SSIS Service
• A platform for ETL operations
• Installed as a feature of SQL Server
• Control flow engine
• Runtime resources and operational support for data flow
• Data flow engine
• Pipeline architecture for buffer-oriented rowset processing
• SSIS Projects
• Organize related packages together
• SSIS Packages
• Control flow definition and data flow definition
• Trigger with dtexec.exe or dtexecui.exe
Demonstration: Working with SSIS
In this demonstration, you will see how to use SSIS
to import a text file into a SQL Server database
The SQL Server Import and Export Wizard
• Simplified interface for creating SSIS packages
for data import and export
• Limited support for transformations
• Packages may be executed immediately, or saved
for later execution with dtexec.exe or
dtexecui.exe
Demonstration: Using the SQL Server Import and
Export Wizard
In this demonstration, you will see how to work
with the SQL Server Import and Export Wizard
Lesson 3: Using bcp and BULK INSERT to Import
Data
• The bcp Utility
• Demonstration: Working with bcp
• The BULK INSERT Statement
• Demonstration: Working with BULK INSERT
• The OPENROWSET Function
• Demonstration: Working with OPENROWSET
The bcp Utility
• Command-line tool to import and export data
bcp AdventureWorks.Sales.Currency out D:\Currency.csv -S MIA-SQL -
T -c -t , -r \n
• Use format files to define data schema:
• Create a format file with the format nul direction
bcp AdventureWorks.Sales.Currency format nul -S MIA-SQL -T -c -t , -
r \n –x –f D:\CurrencyFmt.xml
• Use a format file:
bcp AdventureWorks.Sales.Currency out D:\Currency.csv -S MIA-SQL -
T –f D:\CurrencyFmt.xml
Demonstration: Working with bcp
In this demonstration, you will see how to work
with the bcp utility to:
• Create a format file
• Export data
The BULK INSERT Statement
• Transact-SQL command that provides similar
options to bcp in
• Runs inside the database engine process
• Can be part of a user-defined transaction
BULK INSERT AdventureWorks.Sales.OrderDetail
FROM 'F:\orders\neworders.txt'
WITH
(
FIELDTERMINATOR ='|',
ROWTERMINATOR ='\n'
);
GO
Demonstration: Working with BULK INSERT
In this demonstration, you will see how to import
data with the BULK INSERT statement
The OPENROWSET Function
• SELECT rows from a data file based on a format
file
• Data may then be used in other Transact-SQL
statements
• Import rows from any OLE DB provider
• ad hoc distributed queries server level setting must
be enabled
• OLE DB provider must be configured to allow ad hoc
access
• Import a file as a BLOB into a single column/row
Demonstration: Working with OPENROWSET
In this demonstration, you will see how to import
data using the OPENROWSET function
Lesson 4: Deploying and Upgrading Data-Tier
Applications
• Data-Tier Application Overview
• Deploying Data-Tier Applications
• Performing In-Place Upgrades of Data-Tier
Applications
• Extracting Data-Tier Applications
• Demonstration: Working with Data-Tier
Applications
Data-Tier Application Overview
• DAC
• Logical container for all the objects associated with a
user database
• DAC definition may be packaged into a DACPAC
• Creating a DAC
• Application developers
• DBAs
• DAC registration
• Versioning metadata stored by SQL Server
• BACPAC
• Export or import schema and data
Deploying Data-Tier Applications
• SSMS Deploy Data-Tier Application Wizard
• Minimal customization available
• SqlPackage.exe
• Command-line tool
• Windows PowerShell
• Microsoft.SqlServer.Dac.DacStore class
• Azure Management Portal
• Deploy a DACPAC as an Azure SQL Database
Performing In-Place Upgrades of Data-Tier
Applications
• A DACPAC can be used to carry out an in-place
upgrade of an existing DAC
• Differences are identified
• A script is generated to make the schema of the DAC
match the schema defined in the DACPAC
• Upgrade tools:
• SSMS Upgrade Data-Tier Application Wizard
• SqlPackage.exe
• PowerShell
• Upgrade behavior can be controlled using
settings
Extracting Data-Tier Applications
• Any database may be extracted to a DACPAC
• The source database might only contain objects
supported by DAC
• No contained users
• Export tools:
• SSMS Export Data-Tier Application Wizard
• SqlPackage.exe
• PowerShell
Demonstration: Working with Data-Tier
Applications
In this demonstration, you will see how to use
SSMS wizards to:
• Export a data-tier application
• Import a data-tier application
Lab: Importing and Exporting Data
• Exercise 1: Import Excel Data Using the Import Wizard
• Exercise 2: Import a Delimited Text File Using bcp
• Exercise 3: Import a Delimited Text File Using BULK
INSERT
• Exercise 4: Create and Test an SSIS Package to Extract
Data
• Exercise 5: Deploy a Data-Tier Application
Logon Information
Virtual machine: 20764C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd
Estimated Time: 90 minutes
Lab Scenario
Adventure Works Cycles is a global manufacturer,
wholesaler and retailer of cycle products.
The company 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 delimited text file. You
must import both these files into tables that will
be used by the Accounting team.
Lab Scenario (Continued)
Periodically, the Marketing team requires a list of
prospects that have not been contacted within the
last month. You must create and test a package that
will extract this information to a file for them.
The Accounting team has purchased a new
application for tracking fixed assets. The database
for this application is installed as a data-tier
application. You will install the DACPAC provided by
the application developers.
Lab Review
• What alternative methods to an SSIS package
might you use to export the output of the
Sales.usp_prospect_list stored procedure to a file?
• If the HR.JobCandidate table has included a
column for a resumé in Microsoft Word document
format, which of the following commands could
you use to import the document into a column in
a table?
Module Review and Takeaways
• Review Question(s)
• Best Practice
Course Evaluation
• Your evaluation of this course will help Microsoft
understand the quality of your learning
experience.
• Please work with your training provider to access
the course evaluation form.
• Microsoft will keep your answers to this survey
private and confidential and will use your
responses to improve your future learning
experience. Your open and honest feedback is
valuable and appreciated.