Problem: You’re running an ad-hoc query in a Microsoft SQL Server database with SQL Server Management Studio (SSMS) and need to further analyze the result set in an Excel spreadsheet. How do you export the data?
Solution
SSMS provides several options to export query results to Excel. In this article, we will explore four different methods:
- Copy and paste from the results tab
- Save results as a delimited file
- Saving results directly to a fixed-width .rpt file
- Using the SQL Server Import and Export Wizard
Let’s assume we have a T-SQL query that retrieves a list of product names and models from the AdventureWorksLT2019 database:
USE AdventureWorksLT2019;
GO
SELECT [p].[Name] AS [ProductName],
[pm].[Name] AS [ProductModel]
FROM [SalesLT].[Product] [p]
JOIN [SalesLT].[ProductModel] [pm] ON [p].[ProductModelID] = [pm].[ProductModelID]
ORDER BY [p].[Name];
GO
Method 1: Copy and Paste from Results Tab
This method requires the results to be sent to the default grid output. If the results are not going to the grid, you can change the setting by right-clicking in the Query Window and selecting “Results To” > “Results to Grid” or simply pressing Ctrl+D.
To copy and paste the results into Excel:
- Click the box in the upper left-hand corner of the Results pane to highlight all records
- Click on “Copy with Headers” or press Ctrl+Shift+C
- Open a blank workbook in Excel
- Right-click and select “Paste” or press Ctrl+V
If the columns are concatenated when pasted into Excel, you can use the “Text to Columns” feature to separate them:
- Highlight the column
- Click “Data” from the Ribbon
- Click “Text to Columns”
- In the Text to Columns Wizard, check the “Tab” box and uncheck any others
- Click “Finish”
Method 2: Save Results as a Delimited File
In addition to copying and pasting, SSMS allows you to export the result set to a comma delimited or tab delimited file that can be opened with Excel.
- Right-click in the Results window
- Select “Save Results As…”
- Choose a location to save the file
- Name the file
- Select either comma or tab delimited from the “Save as type” dropdown
- Click “Save”
- Right-click the saved file and select “Open with” > “Excel”
Method 3: Saving Results Directly to a Fixed-Width .rpt File
If you prefer to work with a fixed-width file instead of a delimited file, SSMS provides the functionality to output the results directly to a fixed-width file.
- Right-click in the Query Window
- Select “Results To” > “Results to File” or press Ctrl+Shift+F
- Run the query and a dialog box will appear
- Select the folder where you want to save the file
- Give the file a name
- Click “Save”
- Open the saved file in Excel by clicking “File” > “Open” > “Browse”
- In the “Open” dialog, select “All files” from the “Files of type” dropdown
- Click on the saved file and click “Open”
- The Text Import Wizard will open
- Select “Fixed Width” instead of the default “Delimited”
- Uncheck “My data has headers” if your data does not have headers
- Click “Next” and verify/edit the break line(s)
- Click “Next” and change the data type if needed
- Click “Finish”
- Save the file as a .xlsx file
Method 4: Using the SQL Server Import and Export Wizard
The SQL Server Import and Export Wizard, powered by SQL Server Integration Services (SSIS), allows you to copy data from a source to a destination. In this case, we will use the wizard to export the query results directly to an Excel file.
- Expand “SQL Server” in Object Explorer
- Right-click on the database you’re exporting from
- Select “Tasks” > “Export Data…”
- In the wizard, select the data source from the dropdown
- Confirm or change the server name
- Choose the authentication method
- Confirm or change the database
- Choose a location for the destination file
- Name the file
- Choose the Excel version
- Check or uncheck the “First row has column names” option
- Click “Next” and paste the SQL query (remove “GO” statements)
- Click “Parse” and verify the columns
- Click “Next” and review the actions
- Click “Finish” to start the export
- Check for success and close the wizard
With these methods, you can easily export SQL Server query results to Excel for further analysis and reporting.
Article Last Updated: 2022-07-07