stored procedure to csv

  • Hi

    I need to create a SP and then export the results from it to a CSV file.

    However there seems to be no easy way to do it in SQL server.

    In oracle you can specify the format of it etc.

    In sql server there seems to be so many methods (BCP or SQLCMD) but none which appear to do what I want.

    I just want to write a sp which takes fields from a table and then output those fields to a cvs file. How can I do that?

    Thanks

  • you could do it by running a bcp command inside a stored procedure. you could see examples Here.[/url]

    or search google for - using bcp in stored procedure to create CSV

    -------------------------------------------------
    -Amit
    Give a man a fish and he'll ask for a lemon. Teach a man to fish and he wont get paged on weekends !! :w00t: - desparately trying to fish [/size]

  • Hi

    I have the code below but it is not creating a txt file.

    The procedure runs fine with no errors and i execute it in sql server 2005 query analyzer but it doesnt create the text file. Can anyone spot anything wrong with the code?

    Thanks

    AS

    BEGIN

    select field1,field2 from table;

    SET NOCOUNT ON;

    declare @sql varchar(8000)

    select @sql = 'bcp "exec Textfile "

    queryout C:\result.txt -c -t, -T -S'

    + @@servername

    exec master..xp_cmdshell @sql

    END

    GO

Viewing 3 posts - 1 through 2 (of 2 total)

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