SideBar    Sidebar: An ORDB Hybrid

One step remains before you can run a distributed query against the new Northwinds linked server. By default, distributed queries use current user credentials when connecting to the remote server. You logged in to your server as systems administrator (sa); so SQL Server connects to the Access database as a user called sa. However, the Access database doesn't include an sa user, so the connection fails. The following command solves this problem by telling SQL Server that all local users logged in as sa should connect to the Northwinds server as the remote user called admin, which is the only default user in an Access database:

exec sp_addlinkedsrvlogin
@rmtsrvname = "Northwinds",
@useself = "false",
@locallogin = "sa",
@rmtuser = "Admin",
@rmtpassword = NULL

You're ready to run your first distributed query. Execute the following query, and you'll receive data from the Northwinds database:

SELECT * FROM
 Northwinds...customers

Congratulations, you've just written your first distributed heterogeneous query!

Access doesn't support the four-part name, but you must include the periods as placeholders. Otherwise, SQL Server won't realize you're trying to run a distributed query and will think you're trying to access a local table called customers owned by someone named Northwinds.

You can join tables from Access and SQL Server as if the Access table were a local SQL Server object. Suppose you've created a mobile sales-force automation system and need a query to insert new orders into the master order table when salespeople dock their laptops. The following query uses a left outer join to find remote orders not yet posted to the MasterOrders table. This query inserts those rows into the central MasterOrders table. You'll find little difference between referencing a local table and referencing a remote table:

INSERT into MasterOrders
SELECT
  RemoteOrders.*
FROM
  Northwinds...orders RemoteOrders left join 
  MasterOrders on RemoteOrders.OrderId = 
  MasterOrders.OrderId
  WHERE 
MasterOrders.OrderID = null

Distributed Queries Alternatives
Let's look at OpenRowset and OpenQuery, two Transact-SQL (T-SQL—for an introduction to T-SQL, see Michael D. Reilly, "Managing Data with Query Windows," page 73) functions that you use to execute distributed queries. OpenRowset and OpenQuery are similar; they both support the passthrough of queries to remote data sources. The primary difference between these operations is that OpenRowset requires definition of connection information on the fly, and OpenQuery works with pre-existing linked-server connections.

Distributed Queries Alternatives
The first OpenRowset example, Example 2, issues a simple passthrough using the connection information provided at run time:

SELECT * FROM
OpenRowset("Microsoft.Jet.OLEDB.3.51",
"c:\brian\writing\sqlservermag\nov98\nwind.mdb"; "admin";, "select * from orders")

Figure 1 shows the syntax for an OpenRowset query.

EXAMPLE 3
The second OpenRowset example, Example 3, re-creates the query in Example 1, which you issued using a linked server:

SELECT
RemoteOrders.*
FROM
OpenRowset("Microsoft.Jet.OLEDB.3.51",
"c:\brian\writing\sqlservermag\nov98\nwind.mdb"; "admin"; , 
"select * from orders") RemoteOrders
left join MasterOrders on 
RemoteOrders.OrderId = MasterOrders.OrderId
WHERE
MasterOrders.OrderID = null
Prev. page     1 [2] 3     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Good article .... I have one question ... Does the openrowset execute the stored procedures addlinkedserver, addlinkedsrvlogin, etc ... I need to insert into sql 7 tables from access and the access mdb files will change. I have written a stored procedure that reads a list of tables and can change the linked server info thru the sp_xxx only when the mdb file changes. If I use openrowset and the stored procs are executing behind the scenes then they will run every time which isn't necessary. Any thoughts would be appreciated. Thanks Joe

Joe Finnerty

I don't believe that the following line from the article is correct 'Until NT 5.0 ships, this approach is the only option available if your users are connecting through integrated security. ' It should rather read 'Until NT 5.0 ships, this approach is the only option available if your users are connecting through SQLServer authentication'. Otherwise the writer is contradicting themself in the paragraph below. Using sp_addlinkedsrvlogin with @useself set to TRUE is only valid for SQLServer authentication unless the O/S is Win2000. Further sp_addsrylogin should read sp_addlinkedsrvlogin. The writer should not be addressing 7.0 security as Integrated security. This is a 6.5 feature and they should be discussing in terms of the authentication model. Also, the writer discusses three security options but essentially the first and the third paragraphs are discussing the same ie impersonation.

Paul Williams

Just one question: how can I access e.g. Excel or txt files on remote machines?

Anonymous User

No cacho ni una guea esta caga de arti culo

Anonymous User

Article Rating 1 out of 5

 
 

ADS BY GOOGLE