在T-SQL语句中访问远程数据库(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 本地表
3、自己写的例子
--openrowset使用OLEDB的一些例子
select * from openrowset('SQLOLEDB','Server=(local);PWD=***;UID=sa;','select * from TB.dbo.school') as t
select * from openrowset('SQLOLEDB','Server=(local);PWD=***;UID=sa;',TB.dbo.school) as t
select * from openrowset('SQLOLEDB','Server=(local);Trusted_Connection=yes;',TB.dbo.school) as t
select * from openrowset('SQLOLEDB','(local)';'sa';'***','select * from TB.dbo.school') as t
select * from openrowset('SQLOLEDB','(local)';'sa';'***',TB.dbo.school) as t
select * from openrowset('SQLOLEDB','(local)';'sa';'***','select school.id as id1,people.id as id2 from TB.dbo.school inner join TB.dbo.people on school.id=people.id') as t --openrowset使用SQLNCLI的一些例子(SQLNCLI在SqlServer2005以上才能使用)
select * from openrowset('SQLNCLI','(local)';'sa';'***','select * from TB.dbo.school') as t
select * from openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select * from TB.dbo.school') as tselect * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;','select * from TB.dbo.school') as tselect * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;',TB.dbo.school) as tselect * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;DataBase=TB','select * from dbo.school') as t--openrowset其他使用insert openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select name from TB.dbo.school where id=1') values('ghjkl')/*要不要where都一样,插入一行*/update openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select name from TB.dbo.school where id=1') set name='kkkkkk'delete from openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select name from TB.dbo.school where id=1')--opendatasource使用SQLNCLI的一些例子select * from opendatasource('SQLNCLI','Server=(local);UID=sa;PWD=***;').TB.dbo.school as tselect * from opendatasource('SQLNCLI','Server=(local);UID=sa;PWD=***;DataBase=TB').TB.dbo.school as t--opendatasource使用OLEDB的例子select * from opendatasource('SQLOLEDB','Server=(local);Trusted_Connection=yes;').TB.dbo.school as t--opendatasource其他使用insert opendatasource('SQLNCLI','Server=(local);Trusted_Connection=yes;').TB.dbo.school(name) values('ghjkl')/*要不要where都一样,插入一行*/update opendatasource('SQLNCLI','Server=(local);Trusted_Connection=yes;').TB.dbo.school set name='kkkkkk'delete from opendatasource('SQLNCLI','Server=(local);Trusted_Connection=yes;').TB.dbo.school where id=1--openquery使用OLEDB的一些例子exec sp_addlinkedserver 'ITSV', '', 'SQLOLEDB','(local)' exec sp_addlinkedsrvlogin 'ITSV', 'false',null, 'sa', '***'select * FROM openquery(ITSV, 'SELECT * FROM TB.dbo.school ') --openquery使用SQLNCLI的一些例子exec sp_addlinkedserver 'ITSVA', '', 'SQLNCLI','(local)' exec sp_addlinkedsrvlogin 'ITSVA', 'false',null, 'sa', '***'select * FROM openquery(ITSVA, 'SELECT * FROM TB.dbo.school ') --openquery其他使用insert openquery(ITSVA,'select name from TB.dbo.school where id=1') values('ghjkl')/*要不要where都一样,插入一行*/update openquery(ITSVA,'select name from TB.dbo.school where id=1') set name='kkkkkk'delete openquery(ITSVA,'select name from TB.dbo.school where id=1')
4、总结
可以看到SqlServer连接多服务器的方式有3种
其中我个人认为openrowset最好,使用简单而且支持在连接时制定查询语句使用很灵活
openquery也不错查询时也可以指定查询语句使用也很灵活,不过查询前要先用exec sp_addlinkedserver和exec sp_addlinkedsrvlogin建立服务器和服务器连接稍显麻烦
opendatasource稍显欠佳,他无法在连接时指定查询使用起来稍显笨拙
另外还可以连接到远程Analysis服务器做MDX查询,再用T-Sql做嵌套查询,可见T-SQL的远程查询非常强大
在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)的更多相关文章
- SQL SERVER 导入、导出数据到Exce(使用OpenRowset,、OpenDataSource函数)以及访问远程数据库(openrowset/opendatasource/openquery)
启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务不安 ...
- (转)在T-SQL语句中访问远程数据库
https://www.cnblogs.com/lgx5/p/7821887.html 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasou ...
- 在T-SQL语句中访问远程数据库
1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...
- SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery) 1.启用Ad Hoc Distributed Queries 在使用openrowset ...
- 在T-SQL中访问远程数据库(openrowset、opendatasource、openquery)
1. 启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前要先启用Ad Hoc Distributed Queries服务,因为这个服 ...
- [转]SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
正 文: 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因 ...
- 【SQLServer】使用T-SQL访问远程数据库:openrowset 和 openquery 以及连接服务器的创建
█ 启用/关闭Ad Hoc Distributed QueriesAd Hoc Distributed Queries服务默认是关闭的,要使用openrowset 和 openquery访问远程数据库 ...
- 走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能
原文:走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能 最近公司来一个非常虎的dba 10几年的经验 这里就称之为蔡老师吧 在征得我们蔡老同意的前提下 我们来分享一下蔡老给我们 ...
- sql 2000 无法连接远程数据库 sqlserver不存在或访问被拒绝、不能打开到主机的连接,在端口1433:连接失败等 解决方案
问题: sql 2000 无法连接远程数据库 sqlserver不存在或访问被拒绝 telnet 127.0.0.1 1433 提示:不能打开到主机的连接,在端口1433:连接失败 解决方案: ...
随机推荐
- C# 实现预览dwg文件完整源代码(无需autocad环境)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...
- T4模板_根据DB生成实体类
为了减少重复劳动,可以通过T4读取数据库表结构,生成实体类,用下面的实例测试了一下 1.首先创建一个项目,并添加文本模板: 2.添加 文本模板: 3.向T4文本模板文件添加代码: <#@ tem ...
- TextView之二:常用属性
参考自<疯狂android讲义>2.3节 //TextView所呈现的文字 android:text="我爱Java" //文字颜色 android:textColor ...
- ajax 传值 中文乱码问题
使用encodeURI编码内容 var Path = encodeURI("中文.xls"); url: "ashx/Data.ashx?Path =" + P ...
- js 原型
1: function Person (name,age) { 2: this.name = name; 3: this.age = age; 4: } 5: 6: Person.prototyp ...
- 学习PrintWriter类 .
java.io包 1)首先先知道它的八种构造方法,但怎么记住这八种呢?我们都知道PrintWriter是一种过滤流,也叫处理流.也就是能对字节流和字符流进行处理,所以它会有: PrintWriter( ...
- [C++程序设计]函数的递归调用
在调用一个函数的过程中又出现直接或间接地调用 该函数本身,称为函数的递归(recursive)调用. 包含递归调用的函数称为递归函数. 在实现递归时,在时间和空间上的开销比较大 求n! #includ ...
- Ecstore的微信账号绑定会员免登录
在微信公众号中通过消息交互/自定义菜单,可直接登录Ecstore手机商城.如果想用原商城账号登录,可以通过登录绑定,在后台可以获取登录绑定的链接这个链接可以在微信公众号消息交互场景或自定义菜单中使用, ...
- WPF 资源收集
转载地址:http://www.cnblogs.com/zhoujg/archive/2009/11/04/1596195.html OpenExpressApp的UI现在是使用WPF,所以熟悉WPF ...
- thinkphp项目目录
# ThinkPHP核心文件介绍 ├─ThinkPHP.php 框架入口文件 ├─Common 框架公共文件 ├─Conf 框架配置文件 ├─Extend ...