0

I am using SQL Server 2005. I want to create a stored procedure that will save data into a .csv file.

Below is the query I am trying but it is not creating any file in my system:

use PBMS_DocumationWorkflow
go
create proc s_bcpMasterSysobjects
as
   select   '"' + name + '"'
            + ',' + '"' + convert(varchar(8), crdate, 112) + '"'
            + ',' + '"' + convert(varchar(8), crdate, 108) + '"'
   from master..sysobjects
   order by crdate desc
go
declare @sql varchar(8000)
select @sql = 'bcp "exec PBMS_DocumationWorkflow..s_bcpMasterSysobjects"
                out c:\bcp\sysobjects.txt -c -t, -T -S'+ @@servername
exec master..xp_cmdshell @sql

Please suggest the cause or give me sample code which works fine.

Thanks.

2
  • Are you getting an error message? Is the file just not appearing?
    – Tom H
    Nov 16, 2009 at 14:57
  • it just not showing any error Nov 16, 2009 at 15:01

1 Answer 1

0

Try changing the "out" to "queryout"

3
  • 1
    When I made that change it worked fine for me. Are you sure that xp_cmdshell is enabled on your server?
    – Tom H
    Nov 16, 2009 at 15:29
  • I have already enabled xp_cmdshell for sql server. But still on able to create file however I am able to create file using declare @sql varchar(8000) set @sql = 'OSQL -S EIL-SW-DSK-038\SQLEXPRESS -E -Q "execute AlphaTripNotes.dbo.s_bcpMasterSysobjects" -b -o c:\bcp\sysobjects.csv' exec master..xp_cmdshell @sql, no_output Nov 16, 2009 at 16:08
  • can you please suggest how I can send this file as attachment from sql server. I can have mutiple files for attachment. Nov 16, 2009 at 16:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.