Create T-Sql script to export to csv

  • I would use BCP and you could embed this is SQL script. You need to make sure that XP_CMDSHELL is enabled though if using SQL 2005.

    BCP will allow you to produce CSV files easily. If you want an example let me know.

    KAS

  • Yes, Kelvin's idea is fairly simple. You can use something like:

    DECLARE @cmdstr varchar(100)

    DECLARE @filedir varchar(50)

    SET @filedir = path that the file should be copied to

    SET @cmdstr = 'bcp "SELECT * FROM " QUERYout ' + @filedir + ' -c -t "," -T'

    EXEC master..xp_cmdshell @CMDstr


    Thanks,

    Lori

  • How can I use the BCP with a stored procedure. For example I have a SP "sp1". This sp will provide the result set. Can I transfer the result set in to a csv file?

    Thanks in advance

  • Rather than dig up a 4 year old thread, I think you would be better served by starting a new thread based around your own question, and putting it in the correct forum such as T-SQL or General.

    And try to give a bit more detail and explanation of what you want to do, and what you have tried already.

Viewing 4 posts - 16 through 18 (of 18 total)

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