SQL Server 2008分布式查询示例

1.使用链接服务器

--创建链接服务器
exec sp_addlinkedserver @server='testdb', @srvproduct='', @provider='SQLOLEDB', @datasrc='192.168.0.111'
--更新链接服务器登录映射
exec sp_addlinkedsrvlogin @rmtsrvname='testdb', @useself='false', @locallogin=null, @rmtuser='sa', @rmtpassword='sa123456'
--查看链接服务器信息
select name,product,provider,data_source,query_timeout,lazy_schema_validation,is_remote_login_enabled,is_rpc_out_enabled from sys.servers where is_linked=1
--执行查询
select top 10 * from testdb.Northwind.dbo.Customers
--删除链接服务器登录映射
exec sp_droplinkedsrvlogin @rmtsrvname='testdb', @locallogin=null
--删除链接服务器
exec sp_dropserver @server='testdb'

2.使用特定名称及特定数据源

select top 10 * from opendatasource('SQLOLEDB','Data Source=192.168.0.111;User ID=sa;Password=sa123456').Northwind.dbo.Customers

若执行查询出错,提示:

引用内容 引用内容
SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。

使用下边语句开启/关闭Ad Hoc Distributed Queries组件:

--开启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

参考资料

[1].MSSQl分布式查询:http://www.cnblogs.com/chenkai/archive/2010/09/09/1822305.html

评论: 0 | 引用: 0 | 查看次数: 3556
发表评论
登录后再发表评论!