Tải bản đầy đủ - 0 (trang)
7-16. Routinely Exporting Small XML Datasets

7-16. Routinely Exporting Small XML Datasets

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

Chapter 7 ■ Exporting Data from SQL Server



3.



4.



Add an OLEDB source and double-click to edit. Configure as follows:

OLEDB Connection Manager:



CarSales_OLEDB



Data Access Mode:



SQL Command



SQL Command text:



SELECT XMLCol

FROM (

SELECT ID, ClientName, Country, Town

FROM

CarSales.dbo.Client

FOR XML PATH('Client'), ROOT('RootElement')

) A (XMLCol)



The dialog box should look something like what’s shown in Figure 7-17.



Figure 7-17.  Defining a FOR XML output in an SSIS OLEDB source task

5.



Add a Flat File destination to the Data Flow pane. Connect the OLEDB source

component to it. Double-click to edit.



390

www.it-ebooks.info



Chapter 7 ■ exporting Data from SQL Server



6.



Click New to add a new Flat File connection manager. Give it a name and define

an output file (C:\SQL2012DIRecipes\CH07\ClientExport.Xml in this example).

Make sure that “Column names in first data row” is not checked, but that Unicode is

checked. The dialog box should look like Figure 7-18.



Figure 7-18. XML output as a flat file

7.



Click OK to confirm.



8.



In the Flat File Destination Editor, click Mappings. Make sure that it is only the

XMLColcolumn and that it is mapped between source and destination.



And that is it! Executing this package will export an XML file.



How It Works

All this recipe does is create XML from the data source using FOR XML PATH as a single record. This record is then

written to disk as a text file. The dataset must be small enough to fit into the SSIS buffer.



391

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



7-17. Exporting Data to Excel Using T-SQL

Problem

You want to export data from SQL Server to Excel or Access using T-SQL.



Solution

Use OPENROWSET and OPENDATASOURCE. This is how you do it (C:\SQL2012DIRecipes\CH07\ExcelExport.sql):

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;

Database = C:\SQL2012DIRecipes\CH07\InsertFile.xlsx;', 'SELECT ID, ClientName FROM [Clients$]')

SELECT ID, ClientName FROM dbo.Client;



How It Works

If you wish to export a set of data from SQL Server into Excel with minimal fuss, then using OPENROWSET or

OPENDATASOURCE is a clean and simple solution. All you have to do is run the T-SQL snippet shown earlier.

Exporting to the MS Office suite—by which I mean essentially Access and Excel, but also occasionally

Word—has become a fundamental requirement for all those who develop and manage SQL Server databases. Ad

hoc exports are best handled using OPENROWSET and OPENDATASOURCE. For this to work you must have set up an

Excel worksheet containing the column names in the first row, starting in cell A1, and no data under the column

headers. Also, Ad hoc queries must be enabled on the server—as has been explained previously. Once these

preconditions are met, the rest is a fairly simple piece of T-SQL.

To use OPENDATASOURCE, try the following:

INSERT INTO OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',

'Data Source = C:\SQL2012DIRecipes\CH07\InsertFile.xls;

Extended Properties = Excel 12.0')...[Clients$]

SELECT ID, ClientName FROM dbo.Client

Instead of placing the column headers at the top-left of an Excel worksheet, you can place them anywhere in

any worksheet, and name the range comprising the headers. You can then export using this name instead of the

worksheet names. An example could be—assuming that you have named the headers DestinationRange

(C:\SQL2012DIRecipes\CH07\ExcelRangeExport.sql):

INSERT INTO OPENROWSET(

'Microsoft.ACE.OLEDB.12.0','Excel 12.0; Database = C:\SQL2012DIRecipes\CH07\InsertFile.xlsx;'

, 'SELECT ID, ClientName FROM DestinationRange')

SELECT ID, ClientName FROM dbo.Client



Hints, Tips, and Traps





See Recipe 1-1 for information on how to find and install the OLEDB ACE driver.

Remember to use the 64-bit version in a 64-bit environment.







The column names used in the SELECT clause of the OPENROWSET pass-through query must

match the column names in the spreadsheet.







Data will be appended to any existing rows.



392

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server







You can also use the Microsoft.Jet.OLEDB.4.0 driver, but I advise installing the more

recent ACE driver, even when exporting to older Excel versions. This avoids unexpected

surprises when accidentally using a version of Excel that the older (Jet) driver does not

support. An added advantage is that you do not even have to specify which version of

Excel (8.0, 12.0) you are using, or whether you are using the Office XML-based file format

or the older binary format.







Remember to close the destination file before exporting data.







Note that Excel has a limit on the number of rows that you can export. This was 65,536 up

to the 2003 version and 1,048,576 thereafter.



7-18. Exporting Data to Access Using T-SQL

Problem

You want to export data from SQL Server to Access using T-SQL.



Solution

Use OPENROWSET and OPENDATASOURCE.

To export SQL Server data to Access, run the following T-SQL snippet

(C:\SQL2012DIRecipes\CH07\AccessExportOPENROWSET.sql):

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0',C:\SQL2012DIRecipes\CH07\TestAccess.mdb';

'admin';'',ClientExport)

SELECT ID, ClientName FROM dbo.Client



How It Works

As was the case for Access, OPENROWSET and OPENDATASOURCE allow you to export a subset of data quickly and

easily as part of a T-SQL statement. You need to have created an Access database containing a destination table

ready to receive the data. Ad hoc queries must also be enabled on the server. This snippet inserted rows into the

ClientExport table in the TestAccess.mdb database. Data is appended to any existing rows in the destination table.

To use OPENDATASOURCE, try the following (C:\SQL2012DIRecipes\CH07\AccessExportOpendatasource.sql):

INSERT INTO OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',

'Data Source = C:\SQL2012DIRecipes\CH07\TestAccess.mdb;')...ClientExport

SELECT ID, ClientName FROM dbo.Client;



Hints, Tips, and Traps





You will need to replace 'admin'; and ''; in the OPENROWSET command with a valid

username and password if your Access database is protected.







You can replace the table name with a SELECT query (in single quotes, as with Excel) to

specify the columns in the destination table. Here, too, the column names do not have to

be identical in source and destination, but the column order must match.







See Chapter 1 for details on how to use a database protected with an Access Workgroup

(.mdw) file.



393

www.it-ebooks.info



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

7-16. Routinely Exporting Small XML Datasets

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

×