Exporting data from SQL to CSV file

I have a table in SQL Server 2008 and would like to export it to a csv file. Although I do not know it, there must be some sort of code or script that can be written to accomplish that work and that is what I am looking for. Thanks
SeTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mr KnackeredCommented:
If this is a one off exercise you can use the Export Data functionality within SQL Management Studio. Right click on the database , Tasks, Export Data and export it to Excel or a Flat File. I am using SQL 2014 but think this facility was available in SQL 2008 as well.
0
lcohanDatabase AnalystCommented:
You can use T-SQL to do that like:


USE [AdventureWorks];
GO
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\contact.xls;',
'SELECT * FROM [Sheet1$]')
SELECT TOP 5 FirstName, LastName
FROM Person.Contact
GO



--using BCP with column names:
bcp "select 'ProductID', 'Name', 'ProductNumber' union all select convert(varchar(10), ProductID), Name, ProductNumber from AdventureWorks2008R2.Production.Product" queryout "c:\temp\product.xls" -c -T


--SQL 2005 only
EXEC master..sp_makewebtask
      @outputfile = 'c:\testing.xls',
      @query = 'Select * from dbName.schemaName.tableName',
      @colheaders =1,
      @FixedFont=0,@lastupdated=0,@resultstitle='Testing details'

If you have cmdshel enabled you can do it like:
exec master..xp_cmdshell N'sqlcmd -E -Q"set nocount on; select * from DB_Name.dbo.TableName;" -s"," -W -o"c:\output.csv"'

Or if you need to send it via mail as file attachment please see my answer to a similar question about how you can do that via T-SQL:
http://www.experts-exchange.com/questions/28806321/SQL-to-Excel-Sending.html#a41214757
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZberteocCommented:
If it is only one time issue and you want to do it quickly then just run the select statement from your table and then in the result grid right click on the left upper corner and choose "Save result as", give a name and location and click Save:
save_csv.JPG
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.