您的位置:首页 > 数据库

本地与远程数据库协同操作(数据库分布式查询)

2009-08-26 11:14 197 查看
有时候我们需要将远程两台服务器的数据进行修改、更新等操作。这时希望在一台服务器的方便的查询另一台服务器的表。下面就是操作的方法与步骤:
一、添加、登录远程数据库服务器

exec sp_addlinkedserver
@server='www',--被访问的服务器别名
@srvproduct='',
@provider='SQLOLEDB',
@datasrc='xxx.xxx.xxx.xxx,端口'

EXEC sp_addlinkedsrvlogin
'www', --被访问的服务器别名
'false',
NULL,
'sa', --帐号
'************' --密码

二、使用
1、select * from www.数据库名.dbo.表名
2、

declare @articleId bigint;
declare @uploadtime datetime;
DECLARE MyUserCursor CURSOR FOR
select articleid from AAAAA;

OPEN MyUserCursor;

FETCH NEXT FROM MyUserCursor INTO @articleId;
WHILE @@FETCH_STATUS=0
BEGIN
select @uploadtime=uploadtime from www.XXXXXX.dbo.BBBBBB where fileId=@articleId;
update AAAAA set uploadtime=@uploadtime where articleid=@articleId;
FETCH NEXT FROM MyUserCursor INTO @articleId;
END

CLOSE MyUserCursor;
DEALLOCATE MyUserCursor;

三、移除远程服务器
exec sp_droplinkedsrvlogin @rmtsrvname = 'www', @locallogin = NULL
exec sp_dropserver @server = 'www'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: