Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Text File Contents

Status
Not open for further replies.

togatown

Technical User
Jun 23, 2003
65
US
Most of what I've seen here deals with importing data from external file.

I need to read the entire contents of a text file into a variable.

How can I accomplish this.

Thanks,

Toga
 
You'll need to put it into a table first, then load it up into a variable as needed. You can use a table variable if you'd like, or a temp table. Then select from that table into your variable.

Denny

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(My very old site)
 
Thanks Denny,

I worked this out:

DECLARE @RetCode int , @FileSystem int , @FileHandle int
Declare @FileName varchar(11)
Declare @FileOutput as varchar(8000)

Set @FileName = 'C:\test.htm'

EXECUTE @RetCode = sp_OACreate 'Scripting.FileSystemObject' , @FileSystem OUTPUT

EXECUTE @RetCode = sp_OAMethod @FileSystem, 'OpenTextFile', @FileHandle OUTPUT, @FileName

EXECUTE @RetCode = sp_OAMethod @FileHandle , 'ReadALL' , @FileOutput OUTPUT

EXECUTE sp_OADestroy @FileHandle

EXECUTE sp_OADestroy @FileSystem

Toga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top