1.  启用Ad Hoc Distributed Queries

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

  SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource'的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。
具体启用方法如下:

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. 示例:


--查看当前链接情况 
select * from sys.servers
--使用 sp_helpserver 来显示可用的服务器
exec sp_helpserver

--创建链接服务器 
--使用sp_addlinkedserver来增加链接
exec sp_addlinkedserver
@server = 'ITSV',--被访问的服务器别名
@srvproduct = '',
@provinder = 'SQLOLEDB',
@datasrc = '远程服务器名或ip地址 ' --要访问的服务器
--使用sp_addlinkedsrvlogin 来增加用户登录链接
exec sp_addlinkedsrvlogin
'ITSV', --被访问的服务器别名,要与上面的一致
'false',
null,
'用户名',
'密码' --要连接的服务器对应的用户名和密码
--示例:
exec sp_addlinkedserver 'ITSV', '', 'SQLOLEDB', '远程服务器名或ip地址 '
exec sp_addlinkedsrvlogin 'ITSV', 'false',null, '用户名', '密码' --要连接的服务器对应的用户名和密码
--以后不再使用时删除链接服务器
exec sp_dropserver 'ITSV','droplogins'
-- 删除已经存在的某个链接 
exec sp_droplinkedsrvlogin 'ITSV',Null
exec sp_dropserver 'ITSV' --服务器别名 --查询示例
select * from ITSV.数据库名.dbo.表名 --导入示例
select * into 表 from ITSV.数据库名.dbo.表名
--连接远程/局域网数据(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 --2、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.  范例:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->--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 t
select * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;','select * from TB.dbo.school') as t
select * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;',TB.dbo.school) as t
select * 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 t
select * 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种:

1)openrowset 最好,简单而且支持在连接时制定查询语句,使用灵活

2)openquery 其次,因查询前要先用exec sp_addlinkedserver和exec sp_addlinkedsrvlogin建立服务器和服务器连接稍显麻烦

3)opendatasource 最后,无法在连接时指定查询

另外还可以连接到远程Analysis服务器做MDX查询,再用T-Sql做嵌套查询,可见T-SQL的远程查询非常强大

参考博文

http://www.cnblogs.com/OpenCoder/archive/2010/03/18/1689321.html

在T-SQL中访问远程数据库(openrowset、opendatasource、openquery)的更多相关文章

  1. 在T-SQL语句中访问远程数据库(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. SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)

    SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery) 1.启用Ad Hoc Distributed Queries 在使用openrowset ...

  4. (转)在T-SQL语句中访问远程数据库

    https://www.cnblogs.com/lgx5/p/7821887.html 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasou ...

  5. 在T-SQL语句中访问远程数据库

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

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

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

  7. 【SQLServer】使用T-SQL访问远程数据库:openrowset 和 openquery 以及连接服务器的创建

    █ 启用/关闭Ad Hoc Distributed QueriesAd Hoc Distributed Queries服务默认是关闭的,要使用openrowset 和 openquery访问远程数据库 ...

  8. MSSqlServer访问远程数据库

    --第一部分(要点)--永久访问方式(需对访问远程数据库进行经常性操作)时设置链接数据库Exec sp_addlinkedserver 'MyLinkServer','','SQLOLEDB','远程 ...

  9. DB2 编目并访问远程数据库

    之后将逐步对项目上的DB2相关经验做个总结,梳理一下知识结构. 要远程操作数据库,首先要进行编目,分三个步骤: 1. 在客户端建立服务器端数据库的节点,编目远程节点. 格式如下: 1. CATALOG ...

随机推荐

  1. [转载]WEB缓存技术概述

    [原文地址]http://www.hbjjrb.com/Jishu/ASP/201110/319372.html 引言 WWW是互联网上最受欢迎的应用之一,其快速增长造成网络拥塞和服务器超载,导致客户 ...

  2. html list <==> unformatted list

    两者的区别很大不得不擦呢,任何两个东西的差别都很大,不得混淆.---一个有ul/ol的选择,一个没有

  3. iOS 打包静态类库 lib.a

    iOS 打包静态类库 lib.a 流程 1: xcode---新建项目---iOS-framework&Library-----Cocoa touch Static Library----Ne ...

  4. 慎用GetOpenFileName

    这两天发现了一个小问题,经过一上午的排查终于找到了问题的原因--Windows 7的API函数GetOpenFileName竟然有BUG! 请参考下面的MFC代码: CFileDialog dlg(T ...

  5. Unity5.3官方VR教程-系列1

    如果你不是VR游戏或应用的开发者,可以选择无视下面的内容,这不是给普通用户看的~ 如果你之前曾经为Oculus Rift DK2或者Gear VR开发过,那么心里面一定曾经有千万个草泥马来回奔跑过.虽 ...

  6. stream流批量读取并合并文件

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  7. 如何查看连接mysql的ip地址

    select SUBSTRING_INDEX(host,':',1) as ip , count(*) from information_schema.processlist group by ip;

  8. 【转】使用Web墨卡托辅助球体切片方案的地图公共属性

    原文链接:https://doc.arcgis.com/en/data-appliance/6.1/reference/common-attributes.htm 使用Web墨卡托辅助球体切片方案的地 ...

  9. 【MYSQL】创建虚表来辅助数据库查询

    在进行数据库查询时,有时需要用到对既有的数据表进行多表查询得出的临时条件的数据表,就可以暂时创建成为虚表,并赋予简单明了的字段名以及临时表名. 例题a:查询出每门课程低于平均成绩的学生姓名.课程名称. ...

  10. lambda表達式

    lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 “成为”.运算符将表达式分为两部分,左边指定 ...