|
OpenRowSet w/Parameters
|
Email
|
| Submitted on: |
12/13/2000 9:10:33 PM |
| By: |
Louis Davis
|
| Level: |
Advanced |
| User Rating: |
By 9 Users |
| Compatibility: |
SQL Server 7.0, SQL Server 6.5 and earlier, Other |
| Views: |
50053 |
|
(About the author) |
|
|
|
Steps to use parameters with the OPENROWSET function.
|
| |
Terms of Agreement:
By using this article, you agree to the following terms...
- You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
- You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
- You may link to this article from another website, but ONLY if it is not wrapped in a frame.
- You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
|
One day I was pondering if it would be possible to query a database on another server without having to set up linked servers.The OPENROWSET function seemed like the perfect choice for doing this. It simple takes the parameters of the server name, log in and password with the appropriate select statement.
Example:
SELECT a.*
FROM OPENROWSET('SQLOLEDB', 'ServerName'; 'sa'; '',
SELECT * FROM pubs.dbo.authors ORDER BY au_lname, au_fname') AS a
This statement returns all the row information from the authors table in the pubs database when it is run. Seems this was the answer I was looking for. Yet I had one slight problem. What if I had more than one server I wanted to query?
My first instinct was to just substitute parameters for the values and pull the data from a custom table. But doing this only produced an error. Strange....parameters usually can replace an actual value, why not now? I began to research the issue and found that it is not possible to use OPENROWSET with parameters. In fact, in the Microsoft documentation it clearly states that this is not possible.
Not believing everything I read, I began to experiment and discovered a way to use parameters with the OPENROWSET function. To use parameters, use OPENROWSET within an EXEC() statement.
EXAMPLE:
EXEC('SELECT a.*
FROM OPENROWSET(''SQLOLEDB'', ''' + @ServerName + '''; ''' + @Login + '''; ''' + @Password + ''',
''SELECT * FROM pubs.dbo.authors ORDER BY au_lname, au_fname'') AS a')
Be careful of the apostrophes. Miss one and it will not work. If you have a NULL value for your password, you'll have to change it to a blank character to avoid errors as well. I also found that you need to trim excess spaces from your characters, especially if you are querying a SQL 6.5 Server. SQL considers the extra spaces part of the value and searches for an exact match. Trimming the parameters before passing them with OPENROWSET will avoid any problems.
I have created stored procedures which use this technique and if the Server is online and the parameter information is correct, it works just fine. I wrote other stored procedures to verify a server is on line before querying it, as well as including conditional statements for any errors that might occur. I hope this tweeking of the OPENROWSET function proves useful for one of your applications some day.
|
|
Report Bad Submission
|
Your Vote
|
| |
Other User Comments
|
12/13/2000 9:16:49 PM: Devo
Unbelieveable!!! I guess I'll have to start experimenting myself. (If this comment was disrespectful, please report it.)
|
12/21/2000 2:09:19 AM: qqq
its really good work. thanks (If this comment was disrespectful, please report it.)
|
1/8/2001 11:01:03 AM: DECIS
Very useful :-) (If this comment was disrespectful, please report it.)
|
1/9/2001 4:13:18 AM: mat_in_the_hat
Its a good point for executing *any* function that does no take parameters (using full-text indexed searches springs to mind as something i've used it for before)... One quick comment is to use the system stored procedure sp_executesql rather than EXEC or EXECUTE where possible as it can speed stuff up a bit and offers a little more functionality... Sorry if that sounded like critisism cus the article is exellent! (If this comment was disrespectful, please report it.)
|
7/3/2001 12:11:04 PM: Charles Kincaid
This must be due to the way that statements are parsed. String constants are passed in as a distinct data type that no variable can immitate.
To parameterize this type of command, you have to prepare a single string containing the command and pass it through EXEC so that it re-enters the parser as a constant string.
Hey, anybody want to write a Quick Basic Interpreter that runs as a stored procedure?
(If this comment was disrespectful, please report it.)
|
5/6/2002 4:55:21 AM: Ravi
Excellent Article. Must have born out of need.Experimenting strengthen skills, proved agian. (If this comment was disrespectful, please report it.)
|
5/22/2002 4:19:53 AM: import from dbf files
Have you tried to open also the dbf files using Openrowset function ? Because i intend to import data from dbf in SQL and I cannot manage this
Thank you,Mirela
(If this comment was disrespectful, please report it.)
|
11/23/2002 1:14:29 PM:
Thanks to make our work easier. But I have a question. I want to pass data from SQL to Txt, I'm using OpenRowset and everything works fine, but i don't want my txt show the data with (If this comment was disrespectful, please report it.)
|
2/15/2005 5:47:15 PM:
Thanks. This will make things easier. I have made some changes to use Integrated Security you might like.
Declare @ServerName varchar(50)
set @ServerName = 'k94corpsys'
EXEC ('SELECT a.* FROM OPENROWSET(''MSDASQL'', ''' + 'DRIVER={SQL Server};SERVER=' + @ServerName + ';Integrated Security=SSPI;'',pubs.dbo.authors) AS a ORDER BY au_lname, au_fname')
(If this comment was disrespectful, please report it.)
|
6/20/2005 9:18:36 PM:
it's very resourceful of data (If this comment was disrespectful, please report it.)
|
12/31/2007 9:06:55 AM: Mahsa
Thanks a lot , it's very helpful (If this comment was disrespectful, please report it.)
|
6/1/2008 7:50:17 AM: ifan
hi i'm ifan..i've got the same problem as Mirela in tring to open dbf file in SQL Server 2000... i've tried : - select * from openrowset ('msdasql','driver = {microsoft visual foxpro driver},uid=,datasource = c:\temp\apa';'select * from apa.dbf') - but then error message that ad hoc provider can't use in this connection through linked server or it sounds like that, i forgot.. i'm wondering if u can helping me out,coz i'm stuck here =( thanks for your reply... (If this comment was disrespectful, please report it.)
|
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)
To post feedback, first please login.
|