How to write to textfile in SQL Server 2008

How would I output to a text file to the user's hard drive from SQL Server 2008? In the scenario the user's hard drive is the same hard drive containing the SQL Server.

I don't want the output to be a .csv file or an xml file, just plain text. I also need to do this in the code. I don't want the user to have to manually click export or another GUI button everytime. My plan is to include this functionality in a stored procedure.

Upvotes: 2

Views: 3191

Answers (3)

edrazpgh
edrazpgh

Reputation: 21

Maybe try a BCP out? http://msdn.microsoft.com/en-us/library/ms162802.aspx

Might have an issue getting it directly to the users hard drive depending on the network setup. It might be worth a shot.

bcp TABLENAME out FILEPATH OPTIONS

Upvotes: 1

Aaron Bertrand
Aaron Bertrand

Reputation: 280262

You forgot to mention which version of SQL Server you're using, but assuming SQL Server 2005 or better, I would use SQLCLR for this, not xp_cmdshell or sp_OACreate. See the earliest answer to this question.

Upvotes: 4

Patrick
Patrick

Reputation: 5836

You can do this with sp_OACreate 'Scripting.FileSystemObject'.

Upvotes: 2

Related Questions