Solved

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

Posted on 2012-03-26
7
495 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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

20 Experts available now in Live!

Get 1:1 Help Now