SQL Server 分布式查询

来源:这里教程网 时间:2026-03-02 10:00:23 作者:

场景:

需要访问SQL Server数据库服务器以外的数据源,如Access,Excel或另一台SQL Server服务器。

方法一: 通过链接服务器。

使用sp_addlinkedserver 添加链接服务器名称,链接服务器定义包含定位 OLE DB 数据源所需的全部信息。本地 SQL Server 登录通过使用 sp_addlinkedsrvlogin 映射到链接服务器中的登录。

关于sp_addlinkedserver ,sp_addlinkedsrvlogin 的用法,请查阅SQL Server帮助文档。

方法二:使用OPENROWSET 和 OPENDATASOURCE 函数。

默认情况下,不支持临时名称。DisallowAdhocAccess 访问接口选项必须设置为 0,同时必须启用“临时分布式查询”高级配置选项。

启用启用“临时分布式查询”:

--启用Ad Hoc Distributed Queries

exec sp_configure 'show advanced options',1

reconfigure

exec sp_configure 'Ad Hoc Distributed Queries',1

reconfigure

--关闭Ad Hoc Distributed Queries

exec sp_configure 'Ad Hoc Distributed Queries',0

reconfigure

exec sp_configure 'show advanced options',0

reconfigure

-- 执行以上SQL必须具有数据库管理员权限。

说明:

本文内容适用于SQL Server 2000/2005。

OPENROWSET 和 OPENDATASOURCE 函数的用法参阅SQL Server 帮助文档。

[@more@]

相关推荐