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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CraigLee
  • 21
  • 3

2 Answers2

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
Alan Fisher
  • 2,005
  • 4
  • 41
  • 61
  • 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. – Loganj99 Feb 26 '13 at 19:55
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.