Chapter 7: Automate Repetitive Database Tasks
5.5 Connect to Microsoft SQL Server Data
Although SQL Server is a powerful database management system, it doesn’t have the range of built-in data analysis tools that Excel does. You can use Excel to import data from SQL Server and then use Excel’s wide variety of tools to perform rich data analysis.
Quick Start
To connect to SQL Server data, do the following:
1. In Excel 2007, click Data ➤Get External Data ➤From Other Sources ➤From SQL Server. In Excel 2003, click Data ➤Import External Data ➤Import Data. Then, with the Files of Type list displaying All Files, browse to and select the desired SQL Server con- nection file, or click New Source.
2. Complete the onscreen directions to finish importing the desired SQL Server data table or data view into the current workbook.
How To
To connect to SQL Server data, do the following:
C H A P T E R 5 ■ C O N N E C T TO OT H E R D ATA B A S E S 127
1. In Excel 2007, click Data ➤Get External Data ➤From Other Sources ➤From SQL Server. In Excel 2003, click Data ➤Import External Data ➤Import Data. Then, with the Files of Type list displaying All Files, browse to and select the desired SQL Server con- nection file, or click New Source.
2. If you are using Excel 2007, or you click New Source in Excel 2003, do the following, otherwise skip to step 3:
a. In Excel 2003 only, in the What Kind of Data Source Do You Want to Connect To list, select Microsoft SQL Server, and click Next.
b. In Excel 2007 and Excel 2003, in the Server Name box, type the desired SQL Server instance name.
c. In the Log on Credentials area, do one of the following:
• Click the Use Windows Authentication option if the SQL Server is set up to accept your Windows login name and password to determine the data tables to which you can connect.
• Click the Use the Following User Name and Password option if the SQL Server is set up to accept a specific user name and password to determine the data tables to which you can connect. Type the specific user name and password in the User Name and Password boxes.
d. Click Next.
e. In the Select the Database That Contains the Data You Want list, select the desired database on the SQL Server.
f. In the list of data tables and data views, select the desired data table or data view, and then click Next.
g. On the Save Connection File and Finish page, specify the reusable connection file name and location and optionally a description and search keywords. Then click Finish.
h. In Excel 2003 only, in the Select Data Source dialog box, select the newly created connection file, and click Open.
3. In the Import Data dialog box, do one or more of the following:
• In Excel 2007 only, click the Table, PivotTable Report, PivotChart and PivotTable Report, or Only Create Connection option to view the data as a data table, a Pivot- Table, or a PivotChart and PivotTable, or to create a reusable connection file, respectively.
• Click the Properties button to modify the default data refresh, formatting, and lay- out options.
• In Excel 2003 only, click the Edit Query button to start the Query Wizard and adjust the data as it is being imported into the current workbook.
• Click Existing Worksheet to import the data into the current worksheet, starting with the current worksheet cell.
• Click New Worksheet to create a new worksheet in the current workbook and import the data into the newly created worksheet.
• In Excel 2003 only, click Create a PivotTable Report to create a PivotTable in the current worksheet or a newly created worksheet.
■ Note The preceding procedures assume that you want to import all of the data in the selected data table or the data view into the current Excel workbook. If you want to filter the imported data during the import operation, use Microsoft Query. For more information, see section “5.2: Adjust External Data While Importing.”
Try It
In this exercise, you will practice importing data from a Microsoft SQL Server 2005 Express Edition database table into a new blank Excel workbook.
■ Note This exercise was written using Microsoft SQL Server 2005 Express Edition, SQL Server Manage- ment Studio Express, and the AdventureWorks sample databases installed. To install these components, see Microsoft SQL Server 2005 Express Edition “Download Now!” at http://msdn.microsoft.com/vstudio/
express/sql/download. If you have an edition of SQL Server 2005 that is greater than Express Edition, some of these steps will differ slightly.
Verify that the AdventureWorks sample database is available from SQL Server 2005 Express Edition:
1. Start SQL Server Management Studio Express (click Start ➤All Programs ➤Microsoft SQL Server 2005 ➤SQL Server Management Studio Express).
2. In the Connect to Server dialog box, do the following:
a. In the Server Name list, select the server instance name on which the Adventure- Works sample database should be installed.
b. In the Authentication list, select Windows Authentication or SQL Server Authenti- cation, depending on how you set up the SQL Server to authenticate logins. If you select SQL Server Authentication, enter the user name and password in the User Name and Password boxes.
C H A P T E R 5 ■ C O N N E C T TO OT H E R D ATA B A S E S 129
3. Click Connect.
4. In the Object Explorer pane (if the Object Explorer pane is not visible, click View ➤ Object Explorer), expand the server instance name on which the AdventureWorks sample database should be installed, and expand Databases. A node named AdventureWorks should be visible.
If you do not see an AdventureWorks node, you likely have not yet attached the Adven- tureWorks sample database to SQL Server 2005 Express Edition, and you will not be able to successfully import the data into Excel until you attach the sample database. You can attach the AdventureWorks sample database to SQL Server 2005 Express Edition by doing the following:
1. In the Object Explorer pane, right-click the Databases folder under the server instance name on which the AdventureWorks sample database should be installed, and click Attach.
2. Click the Add button.
3. In the Select the File list, browse to and select the AdventureWorks_Data.mdf file (this file is typically available in the <drive>:\Program Files\Microsoft SQL Server\MSSQL.1\
MSSQL\Data folder), and click OK.
4. Click OK again. The AdventureWorks node appears in the Object Explorer pane.
5. Quit SQL Server Management Studio Express.
Import data from the AdventureWorks sample database into a new blank Excel workbook by doing the following:
1. In Excel 2007, click Data ➤Get External Data ➤From Other Sources ➤From SQL Server, and skip directly to the next step. In Excel 2003, click Data ➤Import External Data ➤Import Data. Click the New Source button. Then, in the What Kind of Data Source Do You Want to Connect To list, select Microsoft SQL Server, and click Next.
2. In the Server Name box, type the server instance name on which the AdventureWorks sample database is installed.
3. In the Log on Credentials area, do one of the following:
• Click the Use Windows Authentication option if the SQL Server is set up to accept your Windows login name and password to determine the data tables to which you can connect.
• Click the Use the Following User Name and Password option if the SQL Server is set up to accept a specific user name and password to determine the data tables to which you can connect. Type the specific user name and password in the User Name and Password boxes.
4. Click Next.
5. In the Select the Database That Contains the Data You Want list, select Adventure- Works.
6. Clear the Connect to a Specific Table check box, and then click Next.
7. On the Save Connection File and Finish page, in the Description box, type SQL Server AdventureWorks Sample Database Connection.
8. In the Search Keywords box, type AdventureWorks, and then click Finish.
9. In Excel 2003 only, in the Select Data Source dialog box, with the reusable connection file ending in AdventureWorks.odc selected, click Open.
10. In the Select Table dialog box, select the Product table, and click OK.
11. Click OK again to finish importing the table’s data into the current worksheet, begin- ning with the selected worksheet cell.