微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLServer中跨服务器跨数据库之间的数据操作

首先必须理解一个概念:

select * from sys.servers         (查看系统表,看原来的服务器名)

要想跨域就必须在以上信息中可以检索到!

怎样添加

--创建链接服务器 
exec  sp_addlinkedserver    'ITSV ' ' ' 'sqlOLEDB ' '远程服务器名或ip地址 ' 
sp_addlinkedsrvlogin   'false ' sql plain" style="list-style:none; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, null '用户名 ' '密码 ' 
 
--查询示例 
select  from  ITSV.数据库名.dbo.表名 
 
--导入示例 
into  表  ITSV.数据库名.dbo.表名 
 
--以后不再使用时删除链接服务器 
sp_dropserver   'droplogins ' 
 
--连接远程/局域网数据(openrowset/openquery/opendatasource) 
--1、openrowset 
 
--查询示例 
openrowset(  'sql服务器名 ' '密码 ' sql plain" style="list-style:none; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,数据库名.dbo.表名) 
 
--生成本地表 
数据库名.dbo.表名) 
 
--把本地表导入远程表 
insert  sql plain" style="list-style:none; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,数据库名.dbo.表名) 
* 本地表 
 
--更新本地表 
update 
set  b.列A=a.列A 
  sql keyword" style="list-style:none; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,数据库名.dbo.表名) as  inner  join  本地表 b 
on  a.column1=b.column1 
 
--openquery用法需要创建一个连接 
 
--首先创建一个连接创建链接服务器 
'远程服务器名或ip地址 ' 
--查询 
FROM  openquery(ITSV,   'SELECT *  FROM 数据库.dbo.表名 '
--把本地表导入远程表 
本地表 
--更新本地表 
b.列B=a.列B 
'SELECT * FROM 数据库.dbo.表名 ' a  
本地表 b  a.列A=b.列A 
 
--3、opendatasource/openrowset 
SELECT   
FROM    opendatasource(  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 '  ).test.dbo.roy_ta 
--把本地表导入远程表 

测试演示:

user master;

exec sp_addlinkedserver   '41f0bcc ',' ','sqlOLEDB ','192.168.7.21' 
exec sp_addlinkedsrvlogin  '41f0bcc ','false ',null,'sa ','q1w2e3e3r45tr4t5' 

select * from [41f0bcc].eStoreDB.dbo.V_standard_ORG

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐