创建链接服务器

分布式查询

 

 

SQL Server所谓的分布式查询(Distributed Query)是能够访问存放在同一部计算机或不同计算机上的SQL Server 或不同种类的数据源, 从概念上来说分布式查询与普通查询区别 它需要连接多个MSSQL服务器也就是具有多了数据源. 实现在服务器跨域或跨服务器访问. 而这些查询是否被使用完全看使用的需要.
--查看链接服务器  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   
--创建链接服务器  exec sp_addlinkedserver   '666宿舍 ', ' ', 'SQLOLEDB ', '192.168.77.251
                         exec sp_addlinkedsrvlogin '666宿舍 ', 'false ',null, 'sa ', '123456
--查询示例  select * from [666宿舍].ycmis.dbo.费用表 
--导入示例  select * into 新表 from [666宿舍].ycmis.dbo.费用表  -
-以后不再使用时删除链接服务器  exec sp_dropserver  '666宿舍 ', 'droplogins '
  --连接远程/局域网数据(openrowset/openquery/opendatasource)  --SQL Server 阻止了对组件 '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、openrowset
  --查询示例  select * from openrowset( 'SQLOLEDB ', '192.168.77.251 '; 'sa '; '123456',ycmis.dbo.费用表) 
--生成本地表  select * intofrom openrowset( 'SQLOLEDB ', '192.168.77.251 '; 'sa '; '123456',ycmis.dbo.费用表) 
--把本地表导入远程表  insert openrowset( 'SQLOLEDB ', '192.168.77.251 '; 'sa'; '123456',ycmis.dbo.表名)  select *from 本地表 
--更新本地表  updateset b.列A=a.列A  from openrowset( 'SQLOLEDB ', '192.168.77.251 '; 'sa '; '123456',ycmis.dbo.费用表)  as a inner join 本地表 b  on a.column1=b.column1 
--openquery用法需要创建一个连接  --首先创建一个连接创建链接服务器  exec sp_addlinkedserver   '666宿舍 ', ' ', 'SQLOLEDB ', '192.168.77.251--查询  selectFROM openquery([666宿舍],  'SELECTFROM ycmis.dbo.费用表 ') 
--把本地表导入远程表  insert openquery([666宿舍],  'SELECTFROM y
cmis.dbo.远程表 ')  select * from 本地表
  --更新本地表  updateset b.列B=a.列B  FROM openquery([666宿舍],  'SELECTFROM ycmis.dbo.费用表 ')  as a   inner join 本地表 b on a.列A=b.列A  --3、opendatasource/openrowset  SELECT   *  FROM   opendatasource( 'SQLOLEDB ',  'Data Source=192.168.77.251;User ID=sa;Password=123456' ).ycmis.dbo.费用表 
--把本地表导入远程表  insert opendatasource( 'SQLOLEDB ',  'Data Source=192.168.77.251;User ID=sa;Password=123456' ).ycmis.dbo.费用表  select * from 本地表

posted on 2012-09-14 22:16  lir198819  阅读(143)  评论(0编辑  收藏  举报

导航