Link to home
Start Free TrialLog in
Avatar of geekboysteves
geekboysteves

asked on

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!

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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 * ')
Avatar of geekboysteves
geekboysteves

ASKER

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...
you might try this one instead:

select * from
OpenRowset('MSDASQL.1', 'Driver={Microsoft Text Driver (*.txt;*.csv)};DefaultDir=C:\Temp\;' , 'select * from MyFile.txt' )
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!
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)
>> 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!
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.
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!
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!