Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-26
7
Medium Priority
?
503 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

886 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