School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,354 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,919 people online right now. Registration is fast and FREE... Join Now!




SQL stored proc dumps to CSV file

 

SQL stored proc dumps to CSV file, export data into csv from mssql 2005 server

corliss

30 Jun, 2009 - 08:33 PM
Post #1

D.I.C Head
Group Icon

Joined: 25 Oct, 2006
Posts: 169



Thanked: 1 times
Dream Kudos: 50
My Contributions
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,

User is offlineProfile CardPM
+Quote Post


reCoded

RE: SQL Stored Proc Dumps To CSV File

1 Jul, 2009 - 08:43 AM
Post #2

D.I.C Regular
***

Joined: 25 Feb, 2008
Posts: 270



Thanked: 5 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: SQL Stored Proc Dumps To CSV File

1 Jul, 2009 - 08:49 AM
Post #3

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,711



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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.gif

Thanks.
User is offlineProfile CardPM
+Quote Post

corliss

RE: SQL Stored Proc Dumps To CSV File

1 Jul, 2009 - 08:03 PM
Post #4

D.I.C Head
Group Icon

Joined: 25 Oct, 2006
Posts: 169



Thanked: 1 times
Dream Kudos: 50
My Contributions
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:
CODE

-- 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: 2 Jul, 2009 - 10:37 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:14PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month