Steps,parameters,with,OPENROWSET,function
Quick Search for:  in language:    
Steps,parameters,with,OPENROWSET,function
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 25,568 lines
 Jobs: 420 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for SQL.
Job to script all Jobs
By Srdjan Josipovic on 6/19


Enumerate SQL Servers using SQLDMO and T-SQL
By Srdjan Josipovic on 6/19


Count Colums
By Usman Farhat on 6/19


Easy travel SQL logins
By Zoltan Tamas, Toth on 6/17


Build a sequence via derived tables
By Michael S. Trachtenberg on 6/15


Click here to see a screenshot of this code!Creating Grids
By Lewis Moten on 6/15

(Screen Shot)

Renumbering identity columns V2.0 in one stored procedure
By Zoltan Tamas, Toth on 6/6


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

OpenRowSet w/Parameters

Print
Email
 

Submitted on: 12/13/2000 9:10:33 PM
By: Louis Davis 
Level: Advanced
User Rating: By 7 Users
Compatibility:SQL Server 7.0, SQL Server 6.5 and earlier, Other

Users have accessed this article 5556 times.
 
(About the author)
 
     Steps to use parameters with the OPENROWSET function.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) 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.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) 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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
12/13/2000 9:16:49 PM:Devo
Unbelieveable!!! I guess I'll have to start experimenting myself.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/21/2000 2:09:19 AM:qqq
its really good work. thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/8/2001 11:01:03 AM:DECIS
Very useful :-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/6/2002 4:55:21 AM:Ravi
Excellent Article. Must have born out of need.Experimenting strengthen skills, proved agian.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.