Articles

Have you ever had the need to export all tables in a database to csv files? Run this command on your database:

The output is a set of bcp (bulk copy commands) in SQL Server. Simply copy these commands in a new notepad document and save it as output.cmd. Please note that some tables may consist of a large number of files so you should have plenty of space on the drive you export the files to.

Change DBNAME to the name of your database.

USE DBNAME
GO
SELECT 'bcp ' +
QUOTENAME(DB_NAME())
+ '.'
+QUOTENAME(SCHEMA_NAME(schema_id))
+ '.'
+QUOTENAME(name)
+ ' '+'out'+' '
+ name + '.csv -w -t"|" -E -S '
+ @@servername
+ ' -T'
FROM sys.objects
WHERE TYPE='u'
AND is_ms_shipped=0