SQL stored proc dumps to CSV file

export data into csv from mssql 2005 server

Page 1 of 1

3 Replies - 3046 Views - Last Post: 01 July 2009 - 09:03 PM

#1 corliss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 189
  • Joined: 25-October 06

SQL stored proc dumps to CSV file

Posted 30 June 2009 - 09:33 PM

HI Guys,
So I was asked today to write a function to take data from a MSSQL 2005 server and create CSV files from query's/stored procs.
Is there an SQL function or keyword that is used to export data sets, or results from query's to files?
p.s. I looked all over the database section and could not find DIC about that.
Thanks,
Is This A Good Question/Topic? 0
  • +

Replies To: SQL stored proc dumps to CSV file

#2 reCoded  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 282
  • Joined: 25-February 08

Re: SQL stored proc dumps to CSV file

Posted 01 July 2009 - 09:43 AM

CSV = comma separated values.

Pull the data you want out of the database and you can use StringBuilder to build your CSV file.

csvData.Append(database.ID + ", ");

Something like that I believe
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: SQL stored proc dumps to CSV file

Posted 01 July 2009 - 09:49 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 corliss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 189
  • Joined: 25-October 06

Re: SQL stored proc dumps to CSV file

Posted 01 July 2009 - 09:03 PM

I am not looking for anyone to do my "homework". What I need to know is if there is a method or function in MS SQL that is used to dump SQL querys into a text file.
I am not sure if my last message is clear or not but again looking for a crumb not the work.
Here is the code that I have so far:
-- Save SQL output to OS file sample code BEGIN
SET NOCOUNT ON
DECLARE @msg varchar(200);
DECLARE @file varchar(200);
DECLARE @cmd varchar(500);
SET @msg = 'Hello, there.';
-- define the output file pathname
SET @file = 'C:\IT\Temp\MyTempTest.txt';
-- >, rewrite the file with the message
SET @cmd = 'echo ' + @msg + ' > ' + @file;
-- NO_OUTPUT, do not return any output to the client
EXEC master..xp_cmdshell @cmd, NO_OUTPUT;
SET @msg = 'How are you.';
-- >>, append the message
SET @cmd = 'echo ' + @msg + ' >> ' + @file;
EXEC master..xp_cmdshell @cmd, NO_OUTPUT;
-- Save SQL output to OS file sample code END



What I was looking for was "EXEC master..xp_cmdshell @cmd, NO_OUTPUT" I had no idea that this existed until i stumbled accross it.
Thanks,

This post has been edited by corliss: 02 July 2009 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1