zoukankan      html  css  js  c++  java
  • 分布式查询

    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 * into 表 from openrowset( 'SQLOLEDB ', '192.168.77.251 '; 'sa '; '123456',ycmis.dbo.费用表)  
     
    --把本地表导入远程表  
    insert openrowset( 'SQLOLEDB ', '192.168.77.251 '; 'sa'; '123456',ycmis.dbo.表名)  
    select *from 本地表  
     
    --更新本地表  
    update b  
    set 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'  
    --查询  
    select *  
    FROM openquery([666宿舍],  'SELECT *  FROM ycmis.dbo.费用表 ')  
     
    --把本地表导入远程表  
    insert openquery([666宿舍],  'SELECT *  FROM ycmis.dbo.远程表 ')  
    select * from 本地表  
    --更新本地表  
    update b  
    set b.列B=a.列B  
    FROM openquery([666宿舍],  'SELECT *  FROM 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 本地表 
     
     
  • 相关阅读:
    交叉工具链的搭建方法(测试成功)
    使用samba实现linux与windows共享(测试成功)
    sd卡脱机烧写系统的方法(测试成功)
    Navicat连接SQLServer未发现数据源名并且未指定默认驱动程序
    使用docker rmi 批量删除docker镜像
    删除镜像docker rmi IMAGE ID提示image is referenced in multiple repositories
    Linux下,改过/etc/profile文件导致ls vi等命令不能使用解决方法
    安装openssl-dev 报错E: Unable to locate package openssl-dev
    zabbix监控redis命中率---张庆沉笔记
    布局之BFC
  • 原文地址:https://www.cnblogs.com/qanholas/p/1867115.html
Copyright © 2011-2022 走看看