In a Stored Procedure, Select from a text or XML file

I need to get a value from a plain, old text file into my stored procedure.  By "text" I mean a word, not like image or binary or blobs or text as in memo (msaccess term).

Like this:

Select aLineOfText as theColumnName
From 'C:\Temp\MyFile.txt'

This could be the contents of the file: "Hello World!"

So when you run it, you get:

TheColumnName
-------------------
Hello World!

1 row(s) affected.

Seems simple.  I must be missing something.

I thought OpenXML might do it, but it doesn't seem to read from files.  This is not part of a DTS package.

Thanks!

geekboystevesAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
it could be easier for the connection parameters, the technique is however the same (using openrowset)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try this:
select * from openrowset('DTSFlatFile'
, 'Data Source=C:\Temp\MyFile.txt
;Mode=Read;Row Delimiter={LF};File Format=0;Column Delimiter=|
;File Type=1;Skip Rows=0;First Row Column Name=False
;Number of Column=0;Max characters per delimited column=255'
,'SELECT * ')
0
 
geekboystevesAuthor Commented:
That looks very promissing!  When I run it, however, I get this:

Server: Msg 7302, Level 16, State 1, Line 1
Could not create an instance of OLE DB provider 'dtsflatfile'.

I am not at the SQL server, so I provided a network path to MyFile.txt.  Other than that, I left your openrowset arguments alone.

I'll see if the SQL DBA can run it and report back...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you might try this one instead:

select * from
OpenRowset('MSDASQL.1', 'Driver={Microsoft Text Driver (*.txt;*.csv)};DefaultDir=C:\Temp\;' , 'select * from MyFile.txt' )
0
 
geekboystevesAuthor Commented:
That's nice!  But...

Server: Msg 7302, Level 16, State 1, Line 1
Could not create an instance of OLE DB provider 'MSDASQL.1'.

I need to try it on my own SQL server, which I can't do right now, but will when I remember to bring in my laptop tomorrow.

Question: When we specify a folder, like C:\Temp, is the procedure going to look on the server's C:\Temp, or the Client's?  (Hoping for Client's)

Thanks!
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
let's try this one:

select * from
OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt;*.csv)};DefaultDir=C:\Temp\;' , 'select * from MyFile.txt' )

the directory will be the one from the server (sorry)
0
 
geekboystevesAuthor Commented:
>> the directory will be the one from the server (sorry)

Well, the file does not exist there, but my friendly DBA put it somewhere the server can see it, so now I have this:

select * from
OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt;*.csv)};DefaultDir=d:\archive\;' , 'select * from MyFile.txt' )

and get this:

Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'MSDASQL' reported an error.  
[OLE/DB provider returned message: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified]

Thanks!
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have to check about the driver, if in the ODBC Administrator it is listed with this or possibly a slightly different name, you have to put the EXACT name of the driver into the query.
0
 
geekboystevesAuthor Commented:
Would this be easier to pull off if the data was in an Access database instead of a text file?  I just want one bit of information (an RC4 key) to be outside the SQL database!  Without the Access database, no RC4 key.

Thanks!
0
 
geekboystevesAuthor Commented:
You certainly pointed me in the right direction with OPENROWSET.   I won't expand the scope of this question, more than I have already, by getting more help troubleshooting my openrowset statements, drivers, permissions and connection parameters.  I think I'll put the information in Access, where it can be encrypted and even password-protected and all sorts of security measures not available in one line of text.

Thanks very much!
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.