Solved

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

Posted on 2012-03-26
7
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

751 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