對于跨不同服務器的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收集整理,歡迎訪問。