SQL中访问远程数据库(MSSQL)

发布时间:2020-07-24 17:39:12 作者:AlunE
来源:网络 阅读:1692

MSSQL访问远程数据库可以使用三种方式(openrowset/opendatasource/openquery):

前提:启用Ad Hoc Distributed Queries
1、启用Ad Hoc Distributed Queries服务(这个服务不安全,SqlServer默认是关闭)
2、启用和关闭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 联机丛书中的 "外围应用配置器"。

使用以下语句开启:

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

使用完毕使用以下语句关闭(这是一个安全隐患):

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

示例:
1、使用创建链接,再使用链接操作的方式/openquery:

--创建链接服务器 
exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用户名 ', '密码 ' 
--查询 
select * from ITSV.数据库名.dbo.表名 
select * FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ') 
--导入 
select * into 表 from ITSV.数据库名.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     --本地表导入远程表
    delete openquery(ITSVA,'select name from 数据库.dbo.表名where id=1')   --删除
exec sp_dropserver  'ITSV ', 'droplogins '    --以后不再使用时删除链接服务器 

2、使用openrowset 连接:


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

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

insert 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    --更新本地表数据,设置本地表数据=远程表数据  

使用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

使用SQLNCLI:

select * from openrowset('SQLNCLI','(local)';'sa';'***','select * from TB.dbo.school') as t
select * from openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select * from 数据库.dbo.表名') as t
select * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;','select * from 数据库.dbo.表名') as t
select * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;',数据库.dbo.表名) as t
select * from openrowset('SQLNCLI','Server=(local);UID=sa;PWD=***;DataBase=数据库','select * from dbo.表名') as t

insert openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select name from 数据库.dbo.表名 where id=1') values('ghjkl')/*要不要where都一样,插入一行*/
update openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select name from 数据库.dbo.表名 where id=1') set name='kkkkkk'
delete from openrowset('SQLNCLI','Server=(local);Trusted_Connection=yes;','select name from 数据库.dbo.表名 where id=1')

3、使用opendatasource:
使用SQLNCLI:

select * from opendatasource('SQLNCLI','Server=(local);UID=sa;PWD=***;').数据库.dbo.表名 as t
select * from opendatasource('SQLNCLI','Server=(local);UID=sa;PWD=***;DataBase=数据库').数据库.dbo.表名 as t
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

使用OLEDB:

    select * from opendatasource('SQLOLEDB','Server=(local);Trusted_Connection=yes;').数据库.dbo.表名 as t
    select * from OpenDataSource('SQLOLEDB','Data Source=服务器;User ID=sa;Password=******').数据库.dbo.表名 as T
推荐阅读:
  1. php如何连接sql_server数据库
  2. sql server 2008如何利用mdf和ldf文件还原数据库

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql 连接 远程

上一篇:C与C++的内存机制的比较

下一篇:获得执行计划方法三-sql_trace

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》