Apart from using four dotted notations, you have three functions to access remote servers. Those functions are OpenQuery, OpenRowSet, and OpenDataSource. OpenDataSource and OpenRowSet commands are by default disabled, and you need to enable these options.
As
in SQL Server 2008, OpenDataSource and OpenRowSet are disabled in SQL
Server 2005 by default. If you want to use these functions, you have to
enable them.
In SQL Server 2005, there is a
dedicated tool called SQL Server Surface Area Configuration. However,
you will not find this tool with the SQL Server 2008 installation.
Instead, there is a feature called Facets to enable OpenDataSource and
OpenRowSet functions.
|
By following the steps in Exercise 1, you can enable these functions.
Exercise 1. Enabling Ad Hoc Remote Queries
Start SQL Server Management Studio from Program Files | SQL Server 2008. Select Database Engine
as Database Type and the SQL Server Database instance name and provide
the necessary user credentials to log in to SQL Server. If you supply
the correct information, you will be taken to SQL Server Management
Studio. Right-click the server and select Facets from the context menu. Select Surface Area Configuration Server from the Facet drop down. As per the RTM version of SQL Server 2008, it is the last facet in the drop down. Enable AdhocRemoteQueriesEnabled as shown in Figure 1.
|
OpenDataSource
Using the OpenDataSource function is the same as using a four-part name to access a Linked Server, except the OpenDataSource()
function defines the link within the function instead of referencing a
predefined linked server. While defining the link in code bypasses the
linked server requirement, if the link location changes, then the change
will affect every query that uses OpenDataSource(). In addition, OpenDataSource() will not accept variables as parameters.
SELECT Name FROM
OpenDataSource ('SQLOLEDB',Data Source ='Dinesh-Mob';User ID = sa;Password =sa')
.Family.dbo.Person
OpenQuery
For pass-through queries, the OpenQuery()
function leverages a Linked Server. So it’s the easiest to develop. It
also handles changes in server configuration with changing code.
SELECT * FROM OPENQUERY(
LinkedServerName,'SELECT * FROM Tour WHERE Country=''Sri Lanka''')
You can do updates as well.
UPDATE OPENQUERY(LinkedServerName,
'SELECT * FROM Tour WHERE Country=''Sri Lanka''')
SET Start ='Colombo'
OpenRowSet
The OpenRowSet
function is the counterpart to the OpenDataSet( ) function. Both
require the remote data source to be fully specified in the distributed
query.
SELECT Name
FROM OPENROWSET('SQLOLEDB',Data Source ='Dinesh-Mob';User ID =
sa;Password =sa',
SELECT * FROM Customers Where Cust = 1')
Warning
There are several
places where you can configure Link Servers. They are Facet, Provider
Options, and Link Server properties. These configurations are different,
and you need to make sure at which place you can do what configuration.