I have a database in SQL Server with a lot of tables and wish to export all tables in csv format. From a very similar question asked previously - Export from SQL Server 2012 to .CSV through Management Studio
Right click on your database in management studio and choose Tasks -> Export Data...
Follow a wizard, and in destination part choose 'Flat File Destination'. Type your file name and choose your options.
What I want is the capability to export all tables at once. The SQL Server Import and Export Wizard only permits one table at a time. This is pretty cumbersome, if you have a very big database. I think a simpler solution might involve writing a query, but not sure.
1st Way
You can replicate the schema to a temp database and then use that database to export all tables (along with column names).
Steps:
1) First, create a script for all the tables:
Tasks->Generate Scripts->all tables->single sql file
2) Create a dummy database and run this script.
3) right click on the dummy database and select tasks->export data-> select source data->select destination as Microsoft Excel and give its path->Execute
You'll get a single spreadsheet with all the tables and its columns.
2nd Way
Execute below query, it'll give all table names in 1st column along with corresponding column names in 2nd column of result.
Copy this result and paste it in CSV.
Instead of clicking
Export Data
, chooseGenerate Scripts
. Select the tables you want, click next and click theAdvanced
button. The last option underGeneral
isTypes of data to script
. ChoseSchema and data
or justData
.Comment on @annem-srinivas solution: If you use schemas other than the default (dbo), change the following in his script:
and
The export wizard allows only one at a time. I used the powershell script to export all my tables into csv. Please try this if it helps you.
Thanks
The answer by sree is great. For my db, because there are multiple schemas, I changed this:
and then also