How to output files from stored procedure to a csv file

  • Hi All,
       I am trying to use the following code to output files from a stored procedure to a csv file or text file but it does not seem to work, Can someone please look at the code and advise me as to what I am doing wrong here. I am using Windows 2012 and SQL Server 2012.

    Thanks
    IQ

    DECLARE @FileName varchar(128),
       @BCP varchar(2048)
    SET @FileName = REPLACE('e:\backup\'+CONVERT(char(8),GETDATE(),112)+'.txt','/','-')
    SET @BCP = 'bcp "exec StarDatabase.dbo.performance_exe" queryout "'
    SET @BCP = @BCP + @FileName + '" -c -t, -S "DELLSTAR\SQL2012" -T'
    PRINT @BCP
    EXEC master.dbo.xp_cmdshell @BCP

  • You'll need to be more specific as to what you mean by " but it does not seem to work".   Did an error occur?  If so, post the entire error message.  Did you even get "Command completed successfully" in SSMS ?  Can you run the code manually, outside of the stored procedure?  Who and/or what executes the stored procedure?

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • And what does the PRINT show for you. Meaning, if you run the cmd from the CLI, what happens?

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

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