zoukankan      html  css  js  c++  java
  • 跨服务器访问SQLSERVER

    一,  设置sp_configure

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

     执行sql :

    SELECT * FROM OPENROWSET ( 'SQLOLEDB ', 'WIN-P8AKPFGRVGZ'; 'sa'; '6YHN7ujm',ReportServer.dbo.batch)

    关闭 把相应设置为0即可。

    二,sp_addlinkedserver

    --创建linkServer
     EXEC SP_ADDLINKEDSERVER  'srv_lnk','','SQLOLEDB','WIN-P8AKPFGRVGZ'
    --登陆linkServer
     EXEC SP_ADDLINKEDSRVLOGIN  'srv_lnk','false',null,'sa','6YHN7ujm'
    --查询linkServer的数据库DataBaseA的表TableA
     SELECT * FROM srv_lnk.ReportServer.dbo.batch
     --List the tables in the linked server
     EXEC sp_tables_ex srv_lnk

    关闭  EXEC SP_DROPSERVER    'srv_lnk', 'droplogins'

    --更新本地表

    update b

    set b.列A=a.列A

    from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b

    on a.column1=b.column1

    --openquery用法需要创建一个连接

    --首先创建一个连接创建链接服务器

    exec sp_addlinkedserver   'WQJK', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '

    --查询

    select *

    FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')

    --把本地表导入远程表

    insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')

    select * from 本地表

    --更新本地表

    update b

    set b.列B=a.列B

    FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a

    inner join 本地表 b on a.列A=b.列A

    --3、opendatasource/openrowset

    SELECT   *

    FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta

    --把本地表导入远程表

    insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名

    select * from 本地表

    注意 远程一定需要开防火墙

  • 相关阅读:
    83. Remove Duplicates from Sorted List
    35. Search Insert Position
    96. Unique Binary Search Trees
    94. Binary Tree Inorder Traversal
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    111. Minimum Depth of Binary Tree
    169. Majority Element
    171. Excel Sheet Column Number
    190. Reverse Bits
  • 原文地址:https://www.cnblogs.com/zxktxj/p/2697893.html
Copyright © 2011-2022 走看看