Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating a text file from within a stored Proc

Posted on 2004-08-20
10
Medium Priority
?
1,401 Views
Last Modified: 2013-11-18
I am using this code (From this web site) to create a text file within a stored procedure. It seems work but the file created is not a text file. it doesn't have .txt extension at the end of the file. How should I  change the code to make the file have the .txt extension. Thanks for help.

DECLARE     @FileSystem int
DECLARE     @FileHandle int
DECLARE @Filename varchar(32)
DECLARE     @text nvarchar(4000)
DECLARE     @RetCode int

SELECT     @text = 'This is some text to write to a file.',
     @Filename = 'C:\MyFile.txt'

EXECUTE @RetCode = sp_OACreate 'Scripting.FileSystemObject' , @FileSystem OUTPUT
IF (@@ERROR|@RetCode > 0 Or @FileSystem < 0)
     RAISERROR ('could not create FileSystemObject',16,1)

EXECUTE @RetCode = sp_OAMethod @FileSystem , 'OpenTextFile' , @FileHandle OUTPUT , @Filename, 2, 1
IF (@@ERROR|@RetCode > 0 Or @FileHandle < 0)
     RAISERROR ('could not create File',16,1)

EXECUTE @RetCode = sp_OAMethod @FileHandle , 'Write' , NULL , @text
IF (@@ERROR|@RetCode > 0 )
     RAISERROR ('could not write to File',16,1)

EXECUTE @RetCode = sp_OAMethod @FileHandle , 'Close'
IF (@@ERROR|@RetCode > 0)
     RAISERROR ('Could not close file ',16,1)

EXEC sp_OADestroy @filehandle
IF (@@ERROR|@RetCode > 0)
     RAISERROR ('Could not destroy file object',16,1)

EXEC sp_OADestroy @FileSystem
0
Comment
Question by:CochiseCounty
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11852465
>>It seems work but the file created is not a text file. it doesn't have .txt extension at the end of the file. <<
I suspect it is a text file, it just does not have the .txt extension.

>>How should I  change the code to make the file have the .txt extension. <<
Try adding '.txt' to the file name?
0
 
LVL 12

Expert Comment

by:kselvia
ID: 11854290
Your code works fine for me

master..xp_cmdshell 'type c:\myfile.txt'

output                              
-------------------------------------
This is some text to write to a file.

Or a quick hack if it will suffice;

declare @sql varchar(255)
set @sql = 'exec master..xp_cmdshell ''echo This is some text to write to a file.>c:\text.txt'''
exec (@sql)
0
 

Author Comment

by:CochiseCounty
ID: 11854489
declare @sql varchar(255)
set @sql = 'exec master..xp_cmdshell ''echo This is some text to write to a file.>c:\text.txt'''
exec (@sql)

This works fine for me too. Thanks. If I want to replace the text 'This is some text to write to a file' with a parameter. How should I change the code.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
Jay Toops earned 1000 total points
ID: 11854724
Like this

declare @sql varchar(255)
declare @myParam varchar(1024)
set @myParam='this is my text now'
set @sql = 'exec master..xp_cmdshell ''echo ' + @myparam + '>c:\text.txt'''
exec (@sql)

Jay
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 11854783
Umm hey thanks for the points but  kselvia deserves at least half the points..
he did most of the work ..

Jay
0
 

Author Comment

by:CochiseCounty
ID: 11854822
I am new to this site and didn't pay attention to points, sorry for that.
0
 
LVL 12

Expert Comment

by:kselvia
ID: 11854875
No problem. Be careful about putting > or | in your text. It will break the echo redirect.  There is also a limit on the length of a dos cmd line, but I don't know what it is.

If you really wanted to avoid all that, do this instead

create table ##mytable (sometext varchar(2000))
insert ##mytable select 'some text to put in a file'
exec master..xp_cmdshell 'bcp "SELECT sometext from tempdb.dbo.##mytable" queryout c:\myfile.txt -c -S<yourservername> -T'
drop table ##mytable
master..xp_cmdshell 'type c:\myfile.txt'
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 11854877
Np.. i just post a request to http://www.experts-exchange.com/Community_Support/
asking to award kselvia half the points for this question ..

Or i can do it if you like...
0
 
LVL 2

Expert Comment

by:jedimike
ID: 20500626

Does anybody know how to set the encoding on this to ANSI?  The file I get by default is Unicode.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20501469
Try posting your own question, instead of piggy-backing on one that is not only closed, but more than 3 years old.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question