Output to CSV or txt file from Query Window

  • Phil Parkin (5/8/2012)


    If you write the results of your query out to a table which is created on the fly (find a way of naming the table uniquely), you can BCP it straight out and then delete the table again.

    True enough... unless two people try to run the same code at nearly the same time. I'm also fairly sure that you can't use a local temp table for this although you can use a table variable.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I have seen some Openrowset , Opendatasource commands in net for reading data from a file directly from a query. Similar way i was trying to find whether we have an option of pumping data to a flat file directly from query.

    So thats another option for you. Which one would you like to try first? In my opinion using SQLCMD via SQL Agent is the simplest solution for dumping a table to CSV.

    I just typed "Use SQLCMD to export a table to a CSV file" into google and got this page.

    http://stackoverflow.com/questions/425379/how-to-export-data-as-csv-format-from-sql-server-using-sqlcmd

    So first get your SQLCMD correct to export your table, then put it into SQL Agent to schedule it if you wish.

Viewing 2 posts - 16 through 16 (of 16 total)

You must be logged in to reply to this topic. Login to reply