zoukankan      html  css  js  c++  java
  • SQL 跨数据库同步数据 、跨数据库跨更新数据

     

    复制代码
      1 --创建链接服务器 
      2 
      3 exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
      4 
      5 exec sp_addlinkedsrvlogin  'ITSV ', 'false ',null, '用户名 ', '密码 ' 
      6 
      7  
      8 
      9 --查询示例 
     10 
     11 select * from ITSV.数据库名.dbo.表名 
     12 
     13  
     14 
     15 --导入示例 
     16 
     17 select * into 表 from ITSV.数据库名.dbo.表名 
     18 
     19  
     20 
     21 --以后不再使用时删除链接服务器 
     22 
     23 exec sp_dropserver  'ITSV ', 'droplogins ' 
     24 
     25  
     26 
     27 --连接远程/局域网数据(openrowset/openquery/opendatasource) 
     28 
     29 --1、openrowset 
     30 
     31  
     32 
     33 --查询示例 
     34 
     35 select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) 
     36 
     37  
     38 
     39 --生成本地表 
     40 
     41 select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) 
     42 
     43  
     44 
     45 --把本地表导入远程表 
     46 
     47 insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) 
     48 
     49 select *from 本地表 
     50 
     51  
     52 
     53 --更新本地表 
     54 
     55 update b 
     56 
     57 set b.列A=a.列A 
     58 
     59  from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b 
     60 
     61 on a.column1=b.column1 
     62 
     63  
     64 
     65 --openquery用法需要创建一个连接 
     66 
     67  
     68 
     69 --首先创建一个连接创建链接服务器 
     70 
     71 exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
     72 
     73 --查询 
     74 
     75 select * 
     76 
     77 FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ') 
     78 
     79 --把本地表导入远程表 
     80 
     81 insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ') 
     82 
     83 select * from 本地表 
     84 
     85 --更新本地表 
     86 
     87 update b 
     88 
     89 set b.列B=a.列B 
     90 
     91 FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a  
     92 
     93 inner join 本地表 b on a.列A=b.列A 
     94 
     95  
     96 
     97 --3、opendatasource/openrowset 
     98 
     99 SELECT   * 
    100 
    101 FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta 
    102 
    103 --把本地表导入远程表 
    104 
    105 insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名 
    106 
    107 select * from
    复制代码

     执行的时候如果报错就执行下面两句  ;

    exec sp_configure 'show advanced options',1
    reconfigure
    exec sp_configure 'Ad Hoc Distributed Queries',1
    reconfigure

    跨表更新表数据

    复制代码
    exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', 'SERVERSQLEXPRESS' --创建远程数据库连接的存储过程
    
    --exec sp_dropserver  'ITSV ', 'droplogins' --  该行不执行  删除存储过程
     
    --更新本地表 
    update b 
    set b.Levels=a.Levels,b.Plogons=a.Plogons
    FROM openquery(ITSV,  'SELECT * FROM LandSection.dbo.LandLevel') as a  
    inner join [dbo].[landlevel] b on a.ID=b.ID
    复制代码
     
     
     
     
     
    如果有执行错误 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

  • 相关阅读:
    2019/9/8
    实现简单的网页登录注册功能 (使用html和css以及javascript技术) 没有美化的日后补全
    测试一些以前的代码
    使用三层开发遵循的原则
    超市管理
    热身训练
    考试第三题
    考试第七题
    考试第10题
    考试第8题
  • 原文地址:https://www.cnblogs.com/kelly1314/p/11454250.html
Copyright © 2011-2022 走看看