SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
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)的更多相关文章
- [转]SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
正 文: 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因 ...
- SQL SERVER 导入、导出数据到Exce(使用OpenRowset,、OpenDataSource函数)以及访问远程数据库(openrowset/opendatasource/openquery)
启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务不安 ...
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...
- sqlserver 跨服务器访问数据
需求:两个一模一样的表,分别分布在两个服务器的数据库上,现在要在一个表中,查看这两个表的内容,并让Id排序 1:在本地数据库查询分析器中,运行以下两段语句: --创建链接服务器 exec sp_add ...
- sqlserver 添加服务器链接 跨服务器访问数据库
转载地址1:https://www.cnblogs.com/wanshutao/p/4137994.html //创建服务器链接 转载地址2:https://www.cnblogs.com/xulel ...
- 问题:sqlserver 跨服务器连接;结果:Sql Server 跨服务器连接
Sql Server 跨服务器连接 用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset ...
- SQL ser 跨实例访问数据库
SqlServer如何跨实例访问数据库 Exec sp_droplinkedsrvlogin LinkName,NullExec sp_dropserver LinkName go EXEC sp_a ...
- SQL Server跨库跨服务器访问实现
我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...
- SQL Server跨服务器操作数据库
今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...
随机推荐
- 关于Java Collections的几个常见问题
列举几个关于Java Collections的常见问题并给出答案. 1. 什么时候用LinkedList,什么时候用ArrayList? ArrayList是使用数组实现的list,本质上就是数组.A ...
- 带索引的tableView
带索引的tableView 一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface Root ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- Hdu1163 Eddy's digitai Roots(九余数定理)
题目大意: 给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下: 例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6: 因为 6 < 10,所 ...
- redis 安装 检测是否安装命令
0: 安装redis服务: # wget http://download.redis.io/releases/redis-3.2.6.tar.gz# tar xzf redis-3.2.6.tar.g ...
- java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec
今天迁移老项目到linux服务器,jdk8 ,tomcat8.5遇到这个问题. java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.J ...
- Andriod——数据存储 SharedPrefrences
xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- java资料——链表(转)
链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两个 ...
- 4种用于构建嵌入式linux系统的工具_转
转自:4种用于构建嵌入式linux系统的工具 Linux 被部署到比 Linus Torvalds 在他的宿舍里开发时所预期的更广泛的设备.令人震惊的支持了各种芯片,使得Linux 可以应用于大大小小 ...
- iOS基础--UIView的常见属性
UIView的常见属性以及方法 @property(nonatomic,readonly) UIView *superview; // 获得自己的父控件对象 @property(nonatomic,r ...