Skip to content

scottgriv/sql-mssql_excel_export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


SQL Server Badge Excel Badge
GitHub Badge Email Badge BuyMeACoffee Badge
Bronze


Export Microsoft SQL Server to Excel

Export data from a Microsoft SQL Server database to an Excel spreadsheet using this query.


Table of Contents

Getting Started

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.

Resources

License

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.

Credits

Author: Scott Grivner
Email: scott.grivner@gmail.com
Website: scottgrivner.dev
Reference: Main Branch