SQL Server CSV Export from Table or Query Data

Introduction

In this article we will learn how to use ZappyShell Command Line Tools for SQL Server CSV Export from Table or Query result

ZappyShell for DB can export your SQL Server data to CSV, PDF, Excel in one line command. This is just single exe less than 5 MB.. CSV export allows you to export data in overwite or append mode. You can specify header, escape sequence, qualifiers and some other advanced options for CSV export.

Download here. Now Lets check how to do this.

Download and Install ZappyShell Command Line Tools

Now next step is we need to Download and Install ZappyShell Command Line tools from hereYou can Download MSI Installer or XCopy Zip file which you can simply extract and copy (see below).

ZappyShell Command Line Tools - Export CSV, Excel, JSON, PDF

ZappyShell Command Line Tools – Export CSV, Excel, JSON, PDF

Download and Install SQL Server Drivers

If you don’t have SQL Server driver installed then just get correct ODBC or OLEDB Driver for Microsoft SQL Server from below link. If you using simple ADO.net Connection string (see below) then no need to install any driver ADO.net driver is standard in .Net Framework.
https://msdn.microsoft.com/en-us/sqlserver/aa937733.aspx?f=255&MSPPError=-2147217396

Sample ADO.net Connection String (No driver needed)

Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;
Data Source=localhost;Initial Catalog=Northwind;User Id=user1;Password=somepwd123;

Sample ODBC Connection String (SQL Native Client Driver)

Driver={SQL Server Native Client 11.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Driver={SQL Server Native Client 11.0};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;
Dsn=mydsn;Uid=myUsername;Pwd=myPassword;

Sample OLEDB Connection String (SQL Native Client Driver)

Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;

More connection strings found here

SQL Server CSV Export – Single table

c:\tools>db.exe export customers --out c:\data\customers.csv --csv --overwrite --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

SQL Server CSV Export – Multiple tables

In the following example we are exporting 3 tables (seperated by vertical bar)
c:\tools>db.exe export customers|orders|products --out c:\data\customers.csv --csv --overwrite --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

SQL Server CSV Export – Multiple tables (using pattern)

In below example all tables where name staring with cust will be exported from sales schema.
c:\tools>db.exe export cust% --schema Sales --out c:\data\customers.csv --csv --overwrite --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

SQL Server CSV Export – SQL Query

In the following example we are exporting output of SQL Query to CSV file. If you have complex query with multi line then you can use Map file with one dataset node.c:\tools>db.exe export "select * from customers where country like 'US%'" --out c:\data\customers.csv --csv --overwrite --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

SQL Server CSV Export – Split Files

Sometimes you may have need to split large amount of data into multiple files. With ZappyShell command line you can split data three different ways. Lets check each example.

SQL Server CSV Export – Split By Row Count

In below example we are exporting SQL Server customer table to CSV files and splitting files with maximum 1000 records per file option.

c:\tools>db.exe export customers --out c:\data\cust.csv --split --splitrows 1000 --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

SQL Server CSV Export – Split By File Size

In below example we are exporting SQL Server customer table to CSV files and splitting files with maximum file size = 10MB option. You also use other size specifier like KB, GB, BYTES etc.

c:\tools>db.exe export customers --out c:\data\cust.csv --split --splitsize 10MB --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

SQL Server CSV Export – Split By Group By Column

In this example we have exported customers in to separate file by country.

c:\tools>db.exe export customers --out c:\data\cust.csv --groupby Country --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"
SQL Server CSV Export - Table to CSV, Split files by Country Column

SQL Server CSV Export – Table to CSV, Split files by Country Column

Exporting Compressed CSV files

If you want to output compressed CSV (GZIP format) then use below command.

c:\tools>db.exe export customers --out c:\data\cust.csv --gzip --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

Using date time place holders in CSV file name

If you want to use date time place holders in file name so it automatically uses specified date time parts use below command simply putting specifiers in file name anywhere.

c:\tools>db.exe export customers --out c:\data\cust_{{YYYY}}_{{MM}}_{{DD}}_{{HH24}}{{MI}}{{SS}}.csv --connstr "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"

Conclusion

In this article we have gone through few ways to use ZappyShell Command Line Tools to export data from SQL Server to CSV. You can also use same command line to export in Excel, JSON, PDF, HTML format.

Reference Links

Posted in SQL Server and tagged , , , , .

Leave a Reply