Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Exporting a csv file via a stored procedure

Author  Topic 

StacyA
Starting Member

2 Posts

Posted - 2008-06-16 : 15:44:39
Reading through the forums, I found some great imformation for importing/exporting an excel spreadsheet via a stored procedure, however, the amount of data I have won't fit in excel. Can someone help me export a CSV file via a stored procedure? I don't know if XML is the answer or if there is another way. I am able to use Bulk for an insert of a csv via stored procudure, although it doesn't allow me to use a variable for the file name and pass it in. We are currently using SQL Server 2000.

Thank you,
Stacy

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-16 : 15:48:41
You can export data using bcp which can be called in a stored procedure with xp_cmdshell:

EXEC master.dbo.xp_cmdshell 'bcp SomeDb.dbo.SomeTable out C:\SomeDir\SomeFile.csv -Sserver1 -T -t, -r\r\n -c'

If you want to build the query, then build it beforehand and then pass it to xp_cmdshell:

DECLARE @cmd nvarchar(4000)

SET @cmd = '...'

EXEC master.dbo.xp_cmdshell @cmd


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -