If, however, you were importing data from another SQL Server, you could select one or more tables or views, or write a query to pull just the data you want.. You can Launch the wizard by
Trang 1writing a query to extract specific data from the source Of course, to write a query the source must support queries when the source has multiple tables or support queries If you are pulling from a flat file (like a csv file), you can get only the data that is in the file If, however, you were importing data from another SQL Server, you could select one or more tables or views, or write a query to pull just the data you want
The paths you can take through the wizard vary greatly with the data sources and destinations you choose The general flow, however, is:
1 Launch the Wizard You can Launch the wizard by clicking the Start menu
| All Programs | Microsoft SQL Server 2008 | Import and Export Data You can also run dtswizard.exe, or right click a database in SSMS’s Object Explorer and choose Tasks | Import Data… or Tasks | Export Data….
2 Choose a Data Source You can use the wizard to import data from flat files, Excel files, Access databases, or any database that you can access via an ODBC, OLE DB, or Net provider Each provider will need to be config-ured differently You will need to have the information that your selected data source requires For example, if you were pulling from a flat file, you would need to know the path of the file, how it was delimited (comma, tab, etc.), and whether or not there was a header row, etc
You can also configure the data source For example, if the source is a flat file, you can use the Advanced page to set appropriate data types for the fields in the text file
3 Choose a Data Destination Just like the data source, this could be a flat file, Excel files, Access databases, or some backend database you connect to using ODBC, OLE DB, or Net providers Again, you will configure the destination provider according to the properties of your target
You can then also configure the destination For example, if the destination is SQL Server, you can choose which database to import data into, or you can even create a new database by using the options in the wizard
4 Select the Source Tables and Views Depending on what kind of source you selected, there may be multiple tables or views in the source that you can import from The wizard will allow you to select one or more of the specific tables and views Or if the data source is support queries, you could instead write a query to extract just the data you want from the source
Trang 2Once you have selected the source objects you can map them to
destination objects If the destination tables already exist, you can select
them, or you can define new tables for the data to be loaded into
The wizard will help to generate the SQL statement to create the target
table You can also select options regarding whether existing data in the
destination table should be kept or cleared, how values for identity
columns should be handled, and so on
5 Review Data Type Mappings There may be some issues with the data types
used for the source and destination columns If there is, the wizard will
show you the Review Data Type Mappings page It will flag the tables and
columns that have problems with a warning icon if there are mapping issues
Where the data types don’t match, you may want to go back and
review the options you selected in the wizard to make sure that you are
mapping the right source columns to the right destination columns If the
source is a flat file, you can also use the advanced properties on the data
source to indicate the correct data types to use as it pulls data from the
source file
You can also use the Review Data Type Mapping page to specify how
the import process should deal with errors that occur You can choose to
ignore the error (which probably means you will get a null for the value
that was invalid), or you could choose to have the error fail the package
You can set global responses for both errors and truncations (the data in
the source is too large for the destination) You can also set the error
han-dling specifically for each column if needed
6 Save and Run the Package You can run and/or save the information you
just configured in the wizard If you choose to save it, the wizard will
create an SSIS package and save it based on the options you choose in the
wizard You can save it as a dtsx file in the file system (this is one of those
XML package files mentioned earlier), or you can save it as data into the
MSDB database on a SQL Server instance When saving it, you also get to
choose how and at what level to encrypt the package If you choose to
save it, the next screen in the wizard will prompt you for the location to
which you want to save the package
The preceding list of steps is a generalization of the process you use as you go
through the wizard You should run through the wizard a few times, selecting
different types of sources and destinations each time so that you can get a feel for
how the wizard works in each situation
Trang 3Again, it is probably better to have you try the wizard than to just fill this book
up with screen shots So give the following exercise a try
EXERCISE 8.7
Using tHE imPort and ExPort Wizard
In this exercise, you will use the Import and Export Wizard to export data from the AdventureWorks2008 database to a flat file This exercise assumes that you have administrative privileges on the SQL Server instance you are working with, that you have the AdventureWorks2008 sample database installed on your SQL Server instance, and that you are running the wizard from the same computer where the SQL Server instance is installed.
1 Since you will be exporting to a file, you need a directory in the file system for the file to be placed into Create a directory off the C: Drive named C:\Wizard.
2 In SQL Server Management Studio, connect to your instance in
the Object Explorer Expand the Databases folder then right click
AdventureWorks2008 From the menu select Tasks | Export
Data….
3 On the wizard’s welcome page, click Next.
4 On the Choose a Data Source page ensure the values match the
ones shown in the following example and then click Next:
■ Data Source SQL Server Native Client 10.0
■ Server Name SQL08
■ Authentication Use Windows Authentication
■ Database AdventureWorks2008
5 On the Choose a Destination page, enter the information shown
in the following example and then click Next.
■ Destination Flat File Destination
■ File Name C:\Wizard\Product.csv
■ Locale (whatever your default value is)
■ Code Page (whatever your default value is) Format Delimited
■ Text qualifier <none>
■ Column names in the first data row Checked
Trang 46 On the Specify Table Copy or Query page, select Copy data from
one or more tables or views and then click Next.
7 On the Configure Flat File Destination page, set the value to
match those shown in the following example After you have set
the values, click the Edit Mappings… and Preview… buttons to
view the information in them Finally, click Next.
Source table or view [Production].[Product]
■
Row delimiter {CR}{LF}
■
Column delimiter Comma {,}
■
8 On the Save and Run Package page, set the values to match
those shown in the following example and then click Next.
■ Run immediately Checked
■ Save SSIS Package Checked
■ SQL Server Selected
■ Package protection level Rely on server storage and roles for
access control
9 On the Save SSIS Package page, set the values to match those
shown in the following example and then click Next.
■ Name Export Product Data
■ Description Exports Production.Product to C:\Wizard\Product.csv
■ Server name (you SQL Server instance name)
■ Use Windows Authentication Selected
10 On the Complete the Wizard page, review the list of actions and
then click Finish.
11 In the SQL Server Import and Export Wizard window, watch as
the process runs It should take only a few seconds When it is
done, you should have a “Success” result You should also see an
action titled “Copying to C:\Wizard\Product.csv” with a status
of “Success” and a message similar to “504 rows transferred.”
If there was a problem, use the information in the window to go
back through the wizard and fix any problems.
12 Verify that the export worked properly by opening the C:\Wizard\
Product.csv file in notepad or Excel and viewing the data.
13 In the Object Explorer in SSMS, click Connect | Integration
Services… In the Connect to Server window enter the name of
your SQL Server in the Server name: box and then click Connect.
Trang 514 To verify that the package was saved correctly, in Object Explorer expand the node for your server’s Integration Services instance,
then click Stored Packages | MSDB | Export Product Data As long
as you see it, it was saved If it isn’t there, you may have selected
a different name or location in the wizard.
Using Business
Intelligence Development Studio
The ultimate way to create SSIS packages is by using Business Intelligence
Development Studio (BIDS) The Copy Database Wizard and the Import and Export Wizard both allow you to create and save SSIS packages that perform
specific actions As your data movement needs become more complex, however, you’ll likely find that the wizards are too limited in what they can do Enter BIDS BIDS is actually the same Visual Studio 2008 development environment that VB Net and C# developers use When you install BIDS as part of your SQL Server installation, you get the Visual Studio development environment (if it isn’t already
on your workstation), and a number of “Business Intelligence Projects” project templates that you can use
BIDS is a developer tool and is normally run from a developer’s workstation, not from the server itself As this book is focused on administrators, not developers,
it isn’t worth digging deep into BIDS However, we will give a quick overview because no discussion of SSIS is complete without covering BIDS As an adminis-trator, you may want to open a package up in BIDS from time to time to learn more about what it does or why problems may be occurring
Let’s look at creating an Integration Services Project in BIDS To get started, follow these steps:
1 Launch BIDS by going to Start menu|All Programs|Microsoft SQL Server 2008 | SQL Server Business Intelligence Development Studio.
2 In BIDS, you will then create a new project from the menu bar by clicking
File|New|Project….