locked
Export to csv file using t-sql from SSMS RRS feed

  • Question

  • Experts,

    I have query as "SELECT * FROM TABLE- NAME", now I want to save the result-set into csv under my C Drive using t-sql (SSMS).

    How to achieve it?

    Thanks in advance

    Kumar


    Please do let us know your feedback. Thank You - KG, MCTS
    Thursday, January 19, 2012 2:58 PM

Answers

All replies

  • EXEC  master..xp_cmdshell 'SQLCMD -S SERVER\SQLSERVERDEV2005 -E -Q "SELECT TOP 10 * FROM pubs.dbo.authors" -b -o c:\myoutput.txt', no_output
    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Thursday, January 19, 2012 3:03 PM
    Answerer
  • Explore bcp with xp_cmdshell. Or if it's one time job, simply save results of a query as a file.
    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog
    Thursday, January 19, 2012 3:04 PM
  • I don't know if there is a way to do it directly into a csv from SSMS, but you have an option to do Results to File. That will generate a .rpt report for you that you can open in Excel and save it as a csv file.
    Abdallah El-Chal, PMP, CIPP/IT, ITIL
    Thursday, January 19, 2012 3:04 PM
  • The best option is the SSIS Import/Export Wizard with query source:

    http://www.sqlusa.com/bestpractices2008/administration/ssiswizardicon/

    Right click on Database in SSMS Object Explorer --> Task --> Export Data.

    Select flat file destination, delimited records. "," is the default delimiter.  


    Kalman Toth, SQL Server & Business Intelligence Training; Beginner Database Design & SQL Programming Using Microsoft SQL Server 2016
    • Marked as answer by Kalman Toth Tuesday, January 24, 2012 6:58 AM
    • Edited by Kalman Toth Thursday, November 9, 2017 8:28 AM
    Tuesday, January 24, 2012 6:51 AM
  • Hello, Please Try using BCP : http://msdn.microsoft.com/en-us/library/ms162802.aspx
    Regards Chaithu.. If it is usefull,Mark this as Answer.
    Tuesday, January 24, 2012 8:56 AM