Tải bản đầy đủ - 0 (trang)
7-21. Exporting Data to Excel Using SSIS

7-21. Exporting Data to Excel Using SSIS

Tải bản đầy đủ - 0trang

Chapter 7 ■ Exporting Data from SQL Server



Figure 7-19.  New worksheet warning

7.



Select the worksheet that you want to use as an Excel destination from the Name of

the Excel Sheet drop-down list.



8.



Click Mappings and map all source to all destination columns.



9.



Click OK.



Your SQL Server data will be exported into the Excel workbook.



How It Works

As probably one of the most-used export processes, outputting data to Excel using SSIS is mercifully simple.

You do not even have to create the destination file first, just be sure to use the Excel destination task. You may

prefer—or need—to use the ACE provider to export data successfully. If this is the case, when creating the

destination OLEDB provider (in step 2) select Microsoft Office 12.0 Access Database Engine OLEDB Provider

from the pop-up list. You will then have to type or paste in the full path to the Access database as the Server or

File name.

You will notice that if your source data contains any non-Unicode (that is to say VARCHAR) columns, then the

destination task will indicate a problem. There are two solutions to this:





Use an SQL statement to select the source data, and use CAST or CONVERT to alter the

source data types to NVARCHAR.







Add a Data Conversion task between the source and the destination, and convert all

non-Unicode columns to Unicode String [DT_WSTR]. It will probably make life easier if

you set the output alias to the original, column name, as well—something like in Figure 7-20.



397

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



Figure 7-20.  Data conversion for Excel export in SSIS



When mapping the columns, ensure that you are mapping the converted columns—named Copy of

ColumnName or DataConversion.ColumnName. If you add the data conversion task before adding the

destination task, then mapping will be a little more complex because both the source and the destination

columns will appear in the Create Table dialog box—and you will have to remove the duplicate (non-Unicode)

columns. Also, this makes it harder for SSIS to map to the correct output data types.



Hints, Tips, and Traps





As with exporting to Office applications, the question of whether you are in a 32-bit or

64-bit environment can have wide-ranging ramifications. These are the same as those

explained in Recipe 1-1.







You can rename the Excel worksheet from OLEDB Destination when you create a new

worksheet, if you prefer.







The intricacies of Excel data type mapping are explained in Recipe 1-3.



398

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



7-22. Exporting Data to Access Using SSIS

Problem

You want to export data to Access as part of an SSIS package.



Solution

Use an OLEDB destination task as part of an SSIS data flow, as follows:

1.



Create a new SSIS package and add an OLEDB connection manager. Name it

CarSales_OLEDB and configure it to connect to your source server.



2.



Add a second OLEDB connection manager. Click New and select Microsoft Jet 4.0

OLEDB Provider from the pop-up list of Providers at the top of the dialog box. Browse

to or type in the path to an existing Access database. Confirm with OK, twice. Name

the second connection manager Access_OLEDB.



3.



Add a Data Flow task and double-click to edit.



4.



Add a new OLEDB source component, and configure it to use the CarSales_OLEDB

connection manager that you added. Select a source table or enter an SQL query to

define the source columns.



5.



Add a new OLEDB destination component. Select the Access_OLEDB connection

manager and either select an existing table as a destination, or click New to create a

new table.



6.



Click Mappings and map the source columns to the destination columns.



7.



Click OK.



You can now run the package.



How It Works

You may prefer—or need—to use the ACE provider to export to Access—specifically if exporting in a 64-bit

environment. When creating the destination OLEDB provider (step 2) in this case, select Microsoft Office 12.0

Access Database Engine OLEDB Provider from the pop-up list. You will then have to type or paste in the full path to

the Access database as the Server or File name. If you are using the ACE provider, you could get a message warning

you about data type problems when creating a new table. In most cases, this can be ignored. What it means is that the

specific Access limited data types will not be used to create the table, but more generic OLEDB data types. Of course,

you can rename any new destination table from OLEDB Destination to a name of your choice when you create it.



Hints, Tips, and Traps





As with exporting to Office applications, the question of whether you are in a 32-bit or

64-bit environment can have wide-ranging ramifications. This is the same as explained

in Recipe 1-1.







If your Access database is password protected, you will need to enter connection details

as part of the OLEDB connection manager configuration.







If your source data contains any non-Unicode (that is to say VARCHAR) columns, then the

destination task will indicate a problem. There are two solutions to this:



399

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server







Use an SQL statement to select the source data and use CAST or CONVERT to alter the

source data types to NVARCHAR.







Add a Data Conversion task between the source and the destination, and convert all

non-Unicode columns to Unicode String [DT_WSTR].



7-23. Pulling Data from Office Applications

Problem

You want to “pull” SQL Server data into Access, Excel, or Word.



Solution

Use the link and/or import options in Access, Excel, and Word.

As there are several ways of pulling data from MS Office applications, I will handle the various scenarios as

individual mini-recipes.



Linking Excel to SQL Server Databases

To link Excel to an SQL Server database:

1.



Click Data to activate the Data ribbon. Select From Other Sources ➤ From SQL Server

(see Figure 7-21).



Figure 7-21.  Linking Excel to an SQL Server database



400

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-21. Exporting Data to Excel Using SSIS

Tải bản đầy đủ ngay(0 tr)

×