[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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!

0
geekboysteves
Asked:
geekboysteves
  • 5
  • 5
1 Solution
 
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
 
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
it could be easier for the connection parameters, the technique is however the same (using openrowset)
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

Featured Post

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!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now