Tải bản đầy đủ - 0 (trang)
7-23. Pulling Data from Office Applications

7-23. Pulling Data from Office Applications

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

Chapter 7 ■ exporting Data from SQL Server


Enter the server name and credentials (if you are not using a trusted connection).

Click Next.


Select the database you wish to connect to. Choose a table or view. Click Next.


Enter a Data Connection file (and path, if necessary). This will allow you to reuse the

connection. Tweak the parameters if you need to. Click Finish.


Select or confirm the top-left destination cell for the data, and click OK. Re-enter the

password if you are using an SQL Server connection, when prompted.

Clicking the Refresh button in the Data ribbon updates the Excel workbook with the latest version of the data

in SQL Server.

Access Linked or Imported Tables

To link Access to an SQL Server data source:


Click External Data to activate the External Data ribbon. Click ODBC databases.


Choose from one of the following:

Import the Source data into a new table in the current database.

Link to the data source by creating a linked table.


Select—or create a DSN (described in Chapter 4 in Recipes 4-6 and 4-10).


Select all the tables you want to link to—or import. Check “Save password” if you are

not using a trusted connection. Click OK.

The source data will be imported into Access.

Linking Word to SQL Server

To use SQL Server data in Word for mailings:


In Word, activate the Mailings ribbon. Click Start Mail Merge. Choose a mailing type,

such as Letters.


Click Select Recipients, and then click Use Existing List, followed by New Source.


In the Data Connection Wizard, select Microsoft SQL Server as the data source, and

then click Next.


Enter the Server and connection credentials (or confirm that integrated security will

be used). Click Next.


Select the source table, and then click Finish.

The source data is now available to be used in Word Mailings.



Chapter 7 ■ Exporting Data from SQL Server

Using PowerPivot

To import data from PowerPivot:


Activate the PowerPivot ribbon. Click PowerPivot Window. PowerPivot will open.


In the Home ribbon, click From Database ➤ From SQL Server. This is shown in

Figure 7-22.

Figure 7-22.  Selecting an SQL Server data sourcefrom the PowerPivot Home ribbon


Define all the connection options (server name, database, authentication mode at a

minimum, plus any other available options that you wish) as in Figure 7-23.



Chapter 7 ■ Exporting Data from SQL Server

Figure 7-23.  Configuration information for a PowerPivot data import


Click Next.


Choose whether to select a table or set of tables (or views), or to write a specific query

to output data, as shown in Figure 7-24.



Chapter 7 ■ Exporting Data from SQL Server

Figure 7-24.  Choosing to import a list of tables or write a query to import data into PowerPivot


Click Next.


Select the table(s) or write the query. An example of this for the CarSales databaseis

shown in Figure 7-25.



Chapter 7 ■ Exporting Data from SQL Server

Figure 7-25.  Selecting tables from the CarSales database


Click Finish. The final pane of the Table Import Wizard will appear, as in Figure 7-26.



Chapter 7 ■ Exporting Data from SQL Server

Figure 7-26.  Selecting tables from the CarSales database


Click Close to close the dialog box once the data is transferred to PowerPivot.

How It Works

Although this chapter has so far dealt with pushing data from SQL server, there are times when you will need to

pull data as well. Normally, I would claim that this is the responsibility of the person handling the destination

application. However, in the case of MS Office products, I have made an exception, and explained some of the

ways in which Excel, Access, and even Word can pull SQL Server data into their respective worksheets, databases,

and documents. The reason that I do this is simple: when a DBA cannot import SQL Server data into his/her



Chapter 7 ■ Exporting Data from SQL Server

MySQL server, it is their problem. When a Microsoft Office user (or even power user) cannot import SQL Server

data into his/her application, it is frequently also a problem for the SQL Server developer or DBA.

To avoid blushing when faced with a user demanding that their data become immediately available, these

are the classic ways to pull SQL Server data into Microsoft Office applications. In these recipes, I am using Office

2010. The techniques are virtually identical to those in Office 2007 or Office 2013. For older Office versions, I can

only suggest that you use this as a template, since the principles are the same; it is only the interface that changes.

Remember that Excel allows you to connect to an SQL Server over ODBC. It will store all workbook connections

you make to a data source, and you can re-use them by clicking the Connections button in the Data ribbon.

For an Analysis Services link, select From Analysis Services in the From Other Sources pop-up. Access also lets

you link to SQL Server—or import data—over ODBC. The process for linking to SQL Server tables—or importing

them—is virtually identical since Access 2007.

There are doubtless many reasons to want to link Microsoft Word to SQL Server databases. It could be

because you have a report that needs data from SQL Server that has to be included and updated as the data

changes, or it could be because you are using Word as a front-end for mailshots (frequently called mailings or

mailmerge). Here again, I explained how to use Word 2007 and up, and leave you to adapt the process to earlier

versions. Oh, and if you are wondering exactly what this has to do with an SQL Server developer, I can only

suggest that you ask for a bottle of wine each time that an end-user wants to use corporate data for a mailshot.

You will soon have a fabulous cellar! Since explaining all the subtleties of actually producing mailshots is outside

the scope of this book, I suggest that you use some of the excellent resources available in book and Web form to

help you from this point on.

Inevitably, PowerPivot, the new kid on the block, can import data from SQL Server and SQL Server Analysis

Services. This can be done from the PowerPivot ribbon. It assumes that PowerPivot has been successfully

installed and that Excel is open.

■■Note The current version of PowerPivot can be found at www.microsoft.com/en-gb/download/

details.aspx?id = 29074.

Hints, Tips, and Traps

One of PowerPivot’s clever options is that it automatically selects all tables (both

immediately upstream and downstream, using foreign key constraints for all selected

tables) if you click the Select Related Tables button at step 7.

The Preview and Filter button allows you to apply data source filters and sorting.

While the Microsoft Word Mailing connection does not allow you to write a custom query,

you can always create a view in SQL server to join tables, aggregate, filter, and sort data

ready for the mailmerge process.

Clicking the Edit Recipient List button (in the Mailings ribbon) displays the Mail Merge

Recipients dialog box that will let you

Filter the dataset.

Sort the source data.

Find duplicates and select the records to use.

Search the source data.



Chapter 7 ■ Exporting Data from SQL Server

7-24. Exporting Files Stored in SQL Server Using T-SQL


You want to export binary files stored in an SQL Server table as part of a scripted T-SQL process.


Use BCP to export binary files with the help of a specific format to handle the export file. The following steps

show how it can be done.


Create a format file exactly like the following (C:\SQL2012DIRecipes\CH07\













Use the following T-SQL to export all the binary files from a

table (C:\SQL2012DIRecipes\CH07\BinaryExport.sql):






@Make VARCHAR(50), @CarPhotoType VARCHAR(5);









ID, Make, CarPhotoType, CarPhoto


CarPhotoType IS NOT NULL;


FETCH NEXT FROM BCPBinaryOUT_CUR INTO @ID, @Make, @CarPhotoType, @CarPhoto



SET @SQLTEXT = 'BCP "SELECT CarPhoto FROM CarSales.dbo.Stock WHERE ID ='

 + CAST(@ID AS VARCHAR(11)) + '" QUERYOUT C:\SQL2012DIRecipes\CH07\' + @Make + '.'

+ @CarPhotoType + '-fC:\SQL2012DIRecipes\CH07\Binary.Fmt -SAdam02 -UAdam –PMe4B0ss';

FETCH NEXT FROM BCPBinaryOUT_CUR INTO @ID, @Make, @CarPhotoType, @CarPhoto

EXECUTE master.dbo.xp_cmdshell







Chapter 7 ■ Exporting Data from SQL Server

How It Works

If you are using SQL Server to store Large OBjects (LOBs), then there will be occasions when you need to export

some, most, or all from a table.

If you are using T-SQL to export a BLOB (Binary Large OBject), then you will need a format file that contains

a single column defining a VARBINARY data type. You then use this format file in a BCP export (BCP OUT). The

SQL here loops through all the records in a table using a cursor, and exports all the BLOBs in the table.

Hints, Tips, and Traps

Ensure that the file names that you create using the dynamic SQL are unique, or the

process will cheerfully overwrite any existing file. Using the unique ID or a combination

of fields that guarantee a unique file name is one method. Failing all else, use an SQL

variable that you increment with each file exported.

All the risks and possible refusals by the DBA as well as the caveats that were mentioned

in Recipe 7-5 concerning xp_cmdshell, apply here, too.

Unfortunately, it does not seem possible to use the newer XML format file to do this.

This approach will only cope with “standard” VARBINARY(MAX) fields, and not FILESTREAM

or FILETABLE files of more than 2 gigabytes in size.

If you are using SQL Server 2008, the format file has to begin with 10.0; for SQL Server

2005, the format file has to begin with 9.0.

7-25. Exporting Files Stored in SQL Server on a Regular Basis


You want to export binary files stored in an SQL Server table as part of a controlled export process.


Use the SSIS Export Column task in a Data Flow to output binary files to disk, as described in the following steps.


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

CarSales_OLEDB and connect to your source database (CarSales in this example).


Add a Data Flow task, double-click to edit, and add an OLEDB source component.

Configure as follows:

OLEDB Connection Manager:


Data Access Mode:

SQL Command

SQL Command Text:

SELECT CarPhoto, 'C:\SQL2012DIRecipes\CH07\'

 + Make + '.'

 + CarPhotoType AS FilePath

FROM dbo.Stock




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

7-23. Pulling Data from Office Applications

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