Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2003, 03:03 AM
Authorized User
 
Join Date: Oct 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SubodhKumar Send a message via Yahoo to SubodhKumar
Default Print value of a variable to text file

Hi All,

declare @str varchar(50)
select @str = 'subodh'
print @str

The above statement declares a variable called @str of varchar type and having the value "subodh" and next statement simply prints the value of @str i.e. "subodh".

Here i want to store/print the value of variable @str to a text file called "s.txt".
can anyone help me to do this through coding which will excute in query analyzer.

My yahoo messenger id is leo_subodh.

Thankx.


Enjoy!
Subodh Kumar
Phoneytunes.com
__________________
<font size=\"2\"><font color=\"blue\"><b>Enjoy!
Subodh Kumar
Phoneytunes.com
</b></font id=\"blue\"></font id=\"size2\">
 
Old December 9th, 2003, 11:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

After most DOS commands you can type >> followed by a filename, the results of the command executing will be appended to the file and nothing will be returned in the DOS box. You can also use the ECHO command to print your own text to the screen, again, if you use >> it is not printed to the screen but appended to the file.

SQL Server has an extended stored procedure called xp_cmdshell. This sproc will execute the text that you pass it as if it were in a DOS box, any results from the command executing will be returned as a recordset unless you supply the no_output parameter, in which case nothing is returned. Therefore to add the result of the @str variable to a text file called s.txt all we need to do is:
Code:
declare @str varchar(50)
select @str = 'subodh'
print @str

select @str = 'ECHO ' + @str + ' >>C:\s.txt'
-- @str will become "ECHO subodh >>C:\s.txt"
EXEC xp_cmdshell @str, no_output
I am sure you can understand the security issues involved in using this sproc, so be very careful in how you use it and what access permissions you give your SQL Server instance (because the sproc inherits it's permissions from the account used to run SQL Server and not the user calling the sproc). You may also need to check that you or someone else has not disabled this sproc for security reasons.

Regards
Owain Williams
 
Old December 10th, 2003, 03:02 AM
Authorized User
 
Join Date: Oct 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SubodhKumar Send a message via Yahoo to SubodhKumar
Default

Thanx Owain , It is working fine.



Enjoy!
Subodh Kumar
Phoneytunes.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Echo / print variable without html code Mantis Pro PHP 4 April 1st, 2005 01:28 PM
Print and print preview file on the website withou appleLover General .NET 0 February 19th, 2005 02:24 AM
Storing Value from Variable into Text File DaBigPolska Beginning VB 6 2 January 7th, 2005 05:34 PM
how to variable keep a value from text file? cslimcom C++ Programming 0 December 26th, 2004 10:13 PM
how to print text file on DMP kumar Pro VB 6 8 May 11th, 2004 12:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.