views:

25

answers:

3

Basically I am looking to select the contents of a pdf into a table.

I am using this query:

SELECT *
FROM   OPENROWSET(BULK N'\\Server\Share\filename.pdf', SINGLE_BLOB) rs

The query won't run because my username doesn't have permissions on that server (nor should it), but I need to be able to authenticate as a different user to execute this query.

It probably won't be an issue in production as I believe the account running the command will have the proper permissions, but as it is, I would like to be able to "impersonate" this user (I obviously have its credentials) and run the query from my account. Any ideas on how to do this?

Thanks...

A: 

Wrap it in a stored procedure and use EXECUTE AS. You'll have to impersonate someone with server rights as well as SQL rights.

http://msdn.microsoft.com/en-us/library/ms188354.aspx

Brad
+1  A: 

You can specify your connection string details in OPENROWSET. See the { 'datasource';'user_id';'password' | 'provider_string' } portion of the syntax below.

OPENROWSET 
( { 'provider_name', { 'datasource';'user_id';'password' 
   | 'provider_string' } 
   , {   [ catalog. ] [ schema. ] object 
       | 'query' 
     } 
   | BULK 'data_file', 
       { FORMATFILE ='format_file_path' [ <bulk_options> ]
       | SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} )<bulk_options> ::=
   [ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ] 
   [ , ERRORFILE ='file_name' ]
   [ , FIRSTROW = first_row ] 
   [ , LASTROW = last_row ] 
   [ , MAXERRORS = maximum_errors ] 
   [ , ROWS_PER_BATCH =rows_per_batch ] 
Joe Stefanelli
@Joe - If he does this in a stored proc, will the credentials be stored in plain text?
JNK
@JNK: Yes they would, but based on the OP's comment that it "won't be an issue in production", I think this is reasonable in a dev. environment.
Joe Stefanelli
Sadly user_id cannot be a Windows login name (which is what I need)
@Joe - I think it's reasonable too, I just wanted to verify.
JNK
@user468341: You want to use the 'provider_string' option in your case, not the 'datasource';'user_id';'password' option. See [connectionstrings.com](http://www.connectionstrings.com/) for help constructing your string.
Joe Stefanelli
Ok, Thanks Joe!
A: 

If you'll be doing it a lot, you can do it as an OSQL command line query (through a batch file or similar):

OSQL -U [username] -p [password] -S [server] -D [database] -q "EXIT(SELECT * FROM OPENROWSET(BULK N'\\Server\Share\filename.pdf', SINGLE_BLOB) rs)"

You need to determine if you want the credentials stored in the database or in a file you can delete once you get to production.

JNK