Export data from a Microsoft SQL Server database to an Excel spreadsheet using this query.
Run the query inside of the excel_export.sql
, changing the following configuration options to your needs:
- xp_cmdshell is a system stored procedure in SQL Server. It allows executing Windows shell commands from the SQL Server environment. While commands are passed as an input string, the shell's output is returned as rows of text.
- Change -S to your DB server and -d to your Database, then change the query to what you want.
- If you want to hide the column headers, add the flag: -h-1 after the .csv" syntax. The only issue is that the dashes are tied to the column header.
- To remove the dashes but keep the header, do a UNION in your query with your column headers.
- 700 is the max width I set so you can adjust it if your columns are smaller/larger. This is for ALL columns.
- I’ve also remove the “Rows Affected” output from the bottom of the file by the “no_output” switch at the end of the command
- Adjust the -o flag to your desired output path and file name.
- Microsoft SQL Server - 2022 Home
- Microsoft SQL Server - 2022 Documentation
- Microsoft SQL Server - Downloads
- Microsoft Excel
This project is released under the terms of The Unlicense, which allows you to use, modify, and distribute the code as you see fit.
- The Unlicense removes traditional copyright restrictions, giving you the freedom to use the code in any way you choose.
- For more details, see the LICENSE file in this repository.
Author: Scott Grivner
Email: [email protected]
Website: scottgrivner.dev
Reference: Main Branch