Skip to content

Instantly share code, notes, and snippets.

@aweigold
Last active December 20, 2015 23:29
Show Gist options
  • Save aweigold/6213154 to your computer and use it in GitHub Desktop.
Save aweigold/6213154 to your computer and use it in GitHub Desktop.
Exporting all tables to CSV in SQL Serverhttp://www.adamweigold.com/2011/11/exporting-all-tables-to-csv-in-sql.htmlThe following script will output a list of statements to use bcp to export all tables to CSV. To use another format, see the bcp documentation.
USE myDatabase
SELECT 'exec master..xp_cmdshell'
+ ' '''
+ 'bcp'
+ ' ' + TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME
+ ' out'
+ ' E:\releasedDB\prod\'
+ TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + '.csv'
+ ' -c'
+ ' -t,'
+ ' -T'
+ ' -S' + @@SERVERNAME
+ ''''
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment