programming4us
programming4us
DATABASE

SQL Server 2008 : Working with Multiple-Source Queries - OpenQuery, OpenRowSet, and OpenDataSource Explained

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
1/20/2011 3:59:14 PM
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.

New & Noteworthy...: Enabling OpenDataSource and OpenRowSet Functions

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
  1. Start SQL Server Management Studio from Program Files | SQL Server 2008.

  2. 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.

  3. Right-click the server and select Facets from the context menu.

  4. 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.

  5. Enable AdhocRemoteQueriesEnabled as shown in Figure 1.

Figure 1. Enable Adhoc Remote Queries from Facets

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.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us