SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)

1、启用Ad Hoc Distributed Queries

在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务不安全所以SqlServer默认是关闭的

启用Ad Hoc Distributed Queries的方法

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的方法,执行下面的查询语句就可以了:

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
使用完毕后,记得一定要要关闭它,因为这是一个安全隐患,切记执行下面的SQL语句

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

2、使用示例

 
--创建链接服务器 
exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用户名 ', '密码 '

--查询示例 
select * from ITSV.数据库名.dbo.表名

--导入示例 
select * into 表 from ITSV.数据库名.dbo.表名

--以后不再使用时删除链接服务器 
exec sp_dropserver  'ITSV ', 'droplogins '

--连接远程/局域网数据(openrowset/openquery/opendatasource) 
--1、openrowset

--查询示例 
select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

--生成本地表 
select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

--把本地表导入远程表 
insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) 
select *from 本地表

--更新本地表 
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   'ITSV ', ' ', '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 本地表

创建存储过程如下

CREATE PROCEDURE [dbo].[P_Syn_SerialNo] 
AS

declare @DataBaseIp varchar(64)        --数据库Ip地址
 declare @DataBasePort nvarchar(64)     --数据库端口号
 declare @DataBaseUser nvarchar(64)     --数据库用户名
 declare @DataBasePassword nvarchar(64) --数据库密码
 declare @DataBaseName nvarchar(64)     --数据库名
 declare @ExcSql varchar(1024)          --执行修改时执行的sql语句

BEGIN

-- 定义游标

declare DataBase_Connection cursor for
 select DataBaseIp,DataBasePort,DataBaseUser,DataBasePassword,DataBaseName
 from T_PS_DATABASE_CONNECTION
 open DataBase_Connection
 fetch next from DataBase_Connection
 into @DataBaseIp,@DataBasePort,@DataBaseUser,@DataBasePassword,@DataBaseName
 while @@fetch_status=0
  begin
   
   -- 启用Ad Hoc Distributed Queries:
   exec sp_configure 'show advanced options',1
   reconfigure
   exec sp_configure 'Ad Hoc Distributed Queries',1
   reconfigure
   
   -- 组装修改语句
   set @ExcSql = 'Data Source=' + @DataBaseIp + ',' + @DataBasePort + ';User ID=' + @DataBaseUser + ';Password=' + @DataBasePassword + ';'
   set @ExcSql =
   'update prisoner ' + 
   'set prisoner.CARDNO = tempTable.serial_no ' +
   'from (SELECT card.serial_no,binding.userNo FROM ' + 
   'OPENDATASOURCE(''SQLOLEDB'', ' +
   '''Data Source=' + @DataBaseIp + ',' + @DataBasePort +
   ';User ID=' + @DataBaseUser + ';Password=' +
   @DataBasePassword + ';''' + ').' + @DataBaseName +
   '.dbo.t_ac_bindingCard binding, ' +
   'OPENDATASOURCE(''SQLOLEDB'', ' +
   '''Data Source=' + @DataBaseIp + ',' + @DataBasePort +
   ';User ID=' + @DataBaseUser + ';Password=' +
   @DataBasePassword + ';''' + ').' + @DataBaseName +
   '.dbo.t_ac_card card ' + 
   'where binding.cardId = card.id) as tempTable ' + 
   'inner join T_PS_NAME prisoner ' + 
   'on tempTable.userNo = prisoner.USERNO'
   
   -- 执行修改语句
   exec (@ExcSql)
   
   
   -- 关闭Ad Hoc Distributed Queries:
   exec sp_configure 'Ad Hoc Distributed Queries',0
   reconfigure
   exec sp_configure 'show advanced options',0
   reconfigure
   
   -- 下一条
   fetch next from DataBase_Connection
   into @DataBaseIp,@DataBasePort,@DataBaseUser,@DataBasePassword,@DataBaseName
  end
 close DataBase_Connection
 deallocate DataBase_Connection
END

SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)的更多相关文章

  1. [转]SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)

    正 文: 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因 ...

  2. SQL SERVER 导入、导出数据到Exce(使用OpenRowset,、OpenDataSource函数)以及访问远程数据库(openrowset/opendatasource/openquery)

    启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务不安 ...

  3. 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)

    1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...

  4. sqlserver 跨服务器访问数据

    需求:两个一模一样的表,分别分布在两个服务器的数据库上,现在要在一个表中,查看这两个表的内容,并让Id排序 1:在本地数据库查询分析器中,运行以下两段语句: --创建链接服务器 exec sp_add ...

  5. sqlserver 添加服务器链接 跨服务器访问数据库

    转载地址1:https://www.cnblogs.com/wanshutao/p/4137994.html //创建服务器链接 转载地址2:https://www.cnblogs.com/xulel ...

  6. 问题:sqlserver 跨服务器连接;结果:Sql Server 跨服务器连接

    Sql Server 跨服务器连接 用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset ...

  7. SQL ser 跨实例访问数据库

    SqlServer如何跨实例访问数据库 Exec sp_droplinkedsrvlogin LinkName,NullExec sp_dropserver LinkName go EXEC sp_a ...

  8. SQL Server跨库跨服务器访问实现

    我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...

  9. SQL Server跨服务器操作数据库

    今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...

随机推荐

  1. paip.中文 分词 ---paoding 3.1 的使用

    paip.中文 分词 ---paoding 3.1 的使用 paoding 3.1 下载: 1 设置字典路径 1 测试代码 1 作者Attilax  艾龙,  EMAIL:1466519819@qq. ...

  2. C# RSA数据加密

    第一步产生密钥类 CreateKey using System; using System.Collections.Generic; using System.Linq; using System.T ...

  3. ubantu 文件系统的目录结构

      对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要, ...

  4. shell echo -n md5sum使用方法

    #!/bin/bash MYSQL='mysql -u*** -p*** -hws5 account' tmp="tmp" resultsource="resultsou ...

  5. static使用实例

    public class LocationActivity extends Activity { //一个Activity传值到service public static String workid ...

  6. hdu2199(高精度二分模版)

    Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and ...

  7. Linux 下 ps 命令

    简述 Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,动态的显示进程信息 ...

  8. 人脸识别技术大总结1——Face Detection & Alignment

    搞了一年人脸识别,寻思着记录点什么,于是想写这么个系列,介绍人脸识别的四大块:Face detection, alignment, verification and identification(re ...

  9. Install Homebrew

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...

  10. Laravel Debugbar

    Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...