1

Is it possible to create a CSV file from a stored procedure?

I need to create a CSV file with data from a table, that is retrieved in a stored procedure

2 Answers 2

2

I got this to work on a simple Stored Procedure, replace "YourDatabase.dbo.YourSproc"

DECLARE @string AS NVARCHAR(4000)

SELECT    @string = 'BCP "exec YourDatabase.dbo.YourSproc" QUERYOUT c:\data.csv -c -T -t'

exec master.dbo.xp_cmdshell @string
Sign up to request clarification or add additional context in comments.

1 Comment

Works good, but you have to add a comma after the '-t'. Otherwise it will just put all of the columns in the same row in excel.
0

The only way I really see this possible is, in the stored procedure, have a call to xp_cmdshell to execute bcp with the parameters you are looking for. Not sure of any other way to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.