Solved

Howto Export from ms-sql database to a csv-file

Posted on 2012-03-26
7
496 Views
Last Modified: 2012-03-27
Hi!

How do i make a query, that selects alot of fields, and export the data
to a csv-file ?
0
Comment
Question by:team2005
7 Comments
 
LVL 13

Expert Comment

by:Ashok
ID: 37766292
If you want to do this using SQL Query Analyzer (or SQL Server Manager Studio),

you can just issue select with fields desired and execute the select

then

right-click on the data grid, select all, Save As
csv-file

HTH
Ashok
0
 
LVL 5

Expert Comment

by:Ronak Patel
ID: 37766358
the sql command  will be like:

sqlcmd -S myServer -d myDB -E -o "MyData.csv" -Q "select column1, column2 from dataTable" -W -w 999 -s","

Open in new window


  -W   remove trailing spaces from each individual field
  -s","   sets the column seperator to the comma (,)
  -w 999   sets the row width to 999 chars
0
 
LVL 2

Author Comment

by:team2005
ID: 37767137
Hi!

Must use stored procedure for this..

How do i do this ?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 13

Expert Comment

by:Ashok
ID: 37767823
Go to following link

http://searchsqlserver.techtarget.com/tip/Stored-procedure-Simplify-comma-separated-value-CSV-exports

it requires free membership signup to view the Stored Procedure.

HTH
Ashok
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37767873
0
 
LVL 3

Accepted Solution

by:
tsnirone earned 500 total points
ID: 37770732
If this is something you'll be doing on a regular basis and/or if the result-set is big I'd use bcp. (Use format-file or view if you're not copying all the columns or if you're joining tables)

http://msdn.microsoft.com/en-us/library/aa174646(v=sql.80).aspx

Easy to code around it to zip & email the file etc

Edit: sqlsrv08 link -> http://msdn.microsoft.com/en-us/library/ms162802.aspx
0
 
LVL 2

Author Closing Comment

by:team2005
ID: 37770752
Thanks
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now