SQL跨数据库同步数据、跨数据库跨更新数据
SQL跨数据库同步数据、跨数据库跨更新数据原地址
1 --创建链接服务器
2
3 exec sp_addlinkedserver  'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
4
5 exec sp_addlinkedsrvlogin  'ITSV ', 'false ',null, '⽤户名 ', '密码 '
6
7
8
9 --查询⽰例
10
11 select * from ITSV.数据库名.dbo.表名
12
13
14
15 --导⼊⽰例
16
17 select * into 表 from ITSV.数据库名.dbo.表名
18
19
20
21 --以后不再使⽤时删除链接服务器
22
23 exec sp_dropserver  'ITSV ', 'droplogins '
24
25
26
27 --连接远程/局域⽹数据(openrowset/openquery/opendatasource)
28
29 --1、openrowset
30
31
32
桃胶的功效与作用吃法33 --查询⽰例
34
35 select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '⽤户名 '; '密码 ',数据库名.dbo.表名)
36
37
38
39 --⽣成本地表
40
41 select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '⽤户名 '; '密码 ',数据库名.dbo.表名)
42
43
44
45 --把本地表导⼊远程表
46
环卫工人的一天
47 insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '⽤户名 '; '密码 ',数据库名.dbo.表名)
48
49 select *from 本地表
50
51
52
53 --更新本地表
54
55 update b
56
57 set b.列A=a.列A
58
59  from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '⽤户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b
60
61 lumn1
62
63
64
65 --openquery⽤法需要创建⼀个连接
66
67
68
69 --⾸先创建⼀个连接创建链接服务器
70
71 exec sp_addlinkedserver  'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
72
73 --查询
鱼肠剑74
75 select *
76
77 FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')
78
79 --把本地表导⼊远程表网上订票取票
80
81 insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')
82
83 select * from 本地表
84
85 --更新本地表
86
87 update b
88
89 set b.列B=a.列B
好玩的魔兽防守地图90
91 FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a
92
93 inner join 本地表 b on a.列A=b.列A
94
95
96
97 --3、opendatasource/openrowset
98
99 SELECT  *
100
101 FROM  opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' )._ta 102
103 --把本地表导⼊远程表缓的组词
104
105 insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名106
107 select * from
执⾏的时候如果报错就执⾏下⾯两句;
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
跨表更新表数据
exec sp_addlinkedserver  'ITSV ', ' ', 'SQLOLEDB ', 'SERVER\SQLEXPRESS' --创建远程数据库连接的存储过程
--exec sp_dropserver  'ITSV ', 'droplogins' --  该⾏不执⾏删除存储过程
--更新本地表
update b
set b.Levels=a.Levels,b.Plogons=a.Plogons
FROM openquery(ITSV,  'SELECT * FROM LandSection.dbo.LandLevel') as a
inner join [dbo].[landlevel] b on a.ID=b.ID
如果有执⾏错误 Ad Hoc Distributed Queries:
启⽤Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。