对于跨不同服务器的sql脚本执行语言的摘要
2024-07-21 02:06:20
供稿:网友
对于相关的数据库脚本的手动执行的数据互导功能!
关键的重点在于建立的连接关系,连接建立好后执行的数据库相关的脚本实现就方便的多了~~~~
1。从一个数据库的一张表b中向另外的一个数据库的表a(起码有相同的字段属性,或字段值)
insert into a(字段1,字段2。。。字段n)
select 字段1,字段2,120,...字段n
from opendatasource('sqloledb','data source=b所在的服务器;user id=用户名;password=登陆口令').b库.dbo.b表
where 字段1=? and ...
该操作一般用在查询分析器中!
2。游标的利用,在服务器间传递数据的值
declare @gen_id nvarchar(4000)
declare @quan nvarchar(4000)
declare @fee_status_operator nvarchar(4000)
declare @fee_status_operatetime nvarchar(4000)
declare @status_operateid nvarchar(4000)
declare @client_sname nvarchar(4000)
begin tran status
declare usr cursor for select d.字段1,d.字段2 from opendatasource(
'sqloledb',
'data source=服务器1;user id=;password='
).库1.dbo.表1 as g inner join opendatasource(
'sqloledb',
'data source=服务器1;user id=;password='
).库1.dbo.表1 as d on g.字段1=d.字段1 inner join opendatasource(
'sqloledb',
'data source=服务器1;user id=;password='
).库2.dbo.表1‘ as s on g.字段1=s.字段1 where g.字段2=0 and g.字段3=0 and s.字段4=6
open usr
fetch next from usr into @gen_id,@quan
while @@fetch_status = 0
begin
update 库a.dbo.表 set 字段[email protected] where 字段1=rtrim(@gen_id)
fetch next from usr into @gen_id,@quan
end
close usr
deallocate usr
declare usr1 cursor for select 字段1,字段2,120,...字段n
from opendatasource(
'sqloledb',
'data source=服务器1;user id=;password='
).库1.dbo.表1 as g inner join opendatasource(
'sqloledb',
'data source=服务器1;user id=;password='
).库1.dbo.表1 as d on g.字段1=d.字段1
where g.字段2=0 and g.字段3=0 and s.字段4=6
open usr1
fetch next from usr into @gen_id,@fee_status_operator,@fee_status_operatetime,@status_operateid,@client_sname
while @@fetch_status = 0
begin
insert into a(字段1,字段2。。。字段n)
values(@gen_id,'2','0',@fee_status_operator,@fee_status_operatetime,@status_operateid,@client_sname)
fetch next from usr1 into @gen_id,@fee_status_operator,@fee_status_operatetime,@status_operateid,@client_sname
end
close usr1
deallocate usr1
----------------说明对于游标的利用可以多次,唯一的缺点的就是执行的时间过长!对于这方面要权衡考虑!
if (@@error<>0)
begin
rollback tran status
end
else
begin
commit tran status
end
------------事物处理
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。