SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Write to a text file from a stored procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

razeena
Starting Member

49 Posts

Posted - 10/19/2011 :  08:08:35  Show Profile  Reply with Quote
Hi,
I have a sp which saves the necessary information
regarding the status of action(whether success or failure, rows affected etc) to a log table
say(StatusLog)After this, I was sending a database mail
with information taken from the log table via sp_send_dbmail. Now I would like to
write the status information to a 'txt' file instead of sending via mail.
How can I write to a text file from a stored procedure in ms sql server 2005?

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/19/2011 :  08:17:19  Show Profile  Reply with Quote
have a look at bcp

http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

razeena
Starting Member

49 Posts

Posted - 10/20/2011 :  07:49:58  Show Profile  Reply with Quote
Thanks.

I tried something like below.

DECLARE @cmd varchar(256), @var varchar(50)
SET @var = 'Hello world!' +CHAR(13)+CHAR(10)+ 'This is second line'
print @var
SET @cmd = 'echo>d:\myfile.txt ' + @var
EXEC master..xp_cmdshell @cmd, no_output

But it is not saving the second line in the file.
How can I manage the line breaks/new lines?


quote:
Originally posted by visakh16

have a look at bcp

http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Edited by - razeena on 10/20/2011 07:50:58
Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 10/20/2011 :  08:13:17  Show Profile  Visit jassi.singh's Homepage  Reply with Quote
Refer following nice article
http://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000