locked
Export data from a select statement RRS feed

  • Question

  • Hello I have a table that contains customer records. I want to be able to run a select statement and have such result exported to a csv on my pc. How can I do this?
    Thursday, May 5, 2011 4:56 PM

Answers

  • try using the bcp utility to get the csv

    bcp "select * from databaseName.schemaName.tablename" queryout D:temp.csv -c -T -t,

    for more information on the command line arguments on the cmd do bcm /? or visit http://msdn.microsoft.com/en-us/library/ms162802.aspx

    Cheers!

    • Proposed as answer by bobragland Thursday, May 5, 2011 7:30 PM
    • Marked as answer by KJian_ Thursday, May 12, 2011 8:57 AM
    Thursday, May 5, 2011 7:29 PM
  • There are several ways to do this.

    SSIS - You can use the import / export tool that comes with SQL Server.  It basically creates a SSIS package that you can store to the file system.  It allows for a custom query as a data source.

    BCP - The one mentioned above is fine also.

    Since we are in the TSQL forum, my choice would be ..

    TSQL - OPENROWSET command uses a OLE DB connector like SSIS.  It can be used as the source for a SELECT statement or target of a DELETE, INSERT, or UPDATE statement.

    Last but not least,

    TSQL - BULKINSERT - does deserve a mention but can only be used for importing data into SQL server.

    • Marked as answer by KJian_ Thursday, May 12, 2011 8:57 AM
    Thursday, May 5, 2011 8:30 PM

All replies

  • hi,

    right click in the result pane, choose 'save result as...'


    Regards, Nico
    Thursday, May 5, 2011 5:02 PM
  • look into osql, a command line utility:

    http://msdn.microsoft.com/en-us/library/ms162806.aspx
    The simpler the solution the stronger it is
    If this post answers you, please mark it as answer..
    If this post is useful, please vote it as useful..
    Thursday, May 5, 2011 5:03 PM
  • try using the bcp utility to get the csv

    bcp "select * from databaseName.schemaName.tablename" queryout D:temp.csv -c -T -t,

    for more information on the command line arguments on the cmd do bcm /? or visit http://msdn.microsoft.com/en-us/library/ms162802.aspx

    Cheers!

    • Proposed as answer by bobragland Thursday, May 5, 2011 7:30 PM
    • Marked as answer by KJian_ Thursday, May 12, 2011 8:57 AM
    Thursday, May 5, 2011 7:29 PM
  • There are several ways to do this.

    SSIS - You can use the import / export tool that comes with SQL Server.  It basically creates a SSIS package that you can store to the file system.  It allows for a custom query as a data source.

    BCP - The one mentioned above is fine also.

    Since we are in the TSQL forum, my choice would be ..

    TSQL - OPENROWSET command uses a OLE DB connector like SSIS.  It can be used as the source for a SELECT statement or target of a DELETE, INSERT, or UPDATE statement.

    Last but not least,

    TSQL - BULKINSERT - does deserve a mention but can only be used for importing data into SQL server.

    • Marked as answer by KJian_ Thursday, May 12, 2011 8:57 AM
    Thursday, May 5, 2011 8:30 PM