As a database administrator by trade, I have had a very perplexing task to have my SQL Server 2005 Enterprise servers communicate with my Oracle 10g box and gather data in real-time in fast, efficient ways. This article will go over the steps to setup the the linked server and some of the gotchas so others have a successful scenario to glean from.
- Install the Oracle Client from Oracle (10.2.x client version) on the SQL Server box
- Setup the TNS to the Oracle box through the Oracle Net Manager application
- Click the ADD (+) icon under Local | Service Naming
- Type in the Service Name, which is used to find in the Service Naming list and the reference to the Service Name in SQL Developer, for example
- Choose the Protocol, which is usually TCP/IP
- Type in the Host Name, which is the Name or IP Address of the Oracle server.
- Type in the Service Name, which is the name of the service or the Global Database Name. It can get confusing between this one and the Service Name above (#1). The difference is that this one (#5) is used to identify the Oracle Name. TNS is a go between. Use (#1) in your app, which then goes through the TNS, which then points to the Service Name (#5) at the database. Another way to look at it is, from your Webserver or SQL Server, directly to Oracle, you go through the TNS using (#1) as the hard coded reference
- Specify the Connection Type, which is either Default or Dedicated Server
- First, turn on the Allow InProcess option
- In SQL Server enterprise manager, expand Server Objects node, and then expand the Providers node.
- Right-click on the OraOLEDB.Oracle provider and click Properties
- Check the Allow Inprocess option (at least this has to be done on my machines)
- Second, create the Oracle linked server
- Right-click on Linked Servers node and click New Linked Server
- Type in the name of the linked server as it would appear in the Linked Server list and as it would be referenced by SQL scripts
- Choose “Other data source” and select the Oracle Provider for Ole DB. If this is not an option, then follow instructions to install the Oracle Client.
- Type in the Product Name, which is the Service Name in #2 of #2 above.
- Type in the Data Source, which is the Service Name in #2 of #2 above.
- In the Security tab, type in the Remote Login and Password under the last option “Be made using this security context:”
- Click OK to commit the changes
- Write the sql query using the following example
- select * from openquery(oraclels, ‘select * from <dbname>.<tablename> where <clause>’)
This doesn’t give the fastest results that we have been able to achieve. There is NOT an easy way to build the query on the fly with variable parameters and have it work quickly. You could create an EXECUTE @SQL statement that has been built around the same query above, but not one that is then in the same batch. The query has to be static and then supply any variable clauses. For example…
select field1, field2
from (select * from openquery (oraclels, ‘select * from db.table where <static clause>’)
where <variable clause>
Hope this helps.