update 的数据是测试机上的,参照正式机的字段恢复,如果一条条人工恢复的话,工程量浩大,因此想到用游标的方法来逐行读取数据进行更新
1.首先连接两个服务器数据库:
exec sp_addlinkedserver '连接名称','','SQLOLEDB','远程服务器名或ip地址'
exec sp_addlinkedsrvlogin '连接名称','false',null,'用户名','密码'
go建立完成之后就可以正常使用了
select * from 连接名称。数据库名称。dbo。表名称 这是一个完整的sql语句
使用完成之后要,删除掉建立的虚拟连接
exec sp_dropserver '连接名称','droplogins'
得到正式机上的资料后逐行读取。并更新测试机上的数据。
Create proc ’存储过程名‘
AS
BEGIN
declare @h_user nvarchar(100)
declare @snumber nvarchar(100)
declare @h_factory nvarchar(100)
declare @h_factory_id nvarchar(100)
declare @htype nvarchar(50)
declare getuser cursor for
select [H_user],[SNumber],[H_factory],[H_Factory_Id],[H_type] from ’连接名称‘.数据库名.表名
open getuserfetch next from getuser into @h_user,@snumber,@h_factory,@h_factory_id,@htype
while(@@fetch_status=0)
begin
Update dbo.C_User set H_user=@h_user where SNumber=@snumber and H_factory=@h_factory and H_Factory_Id=@h_factory_id and H_type=@htype
fetch next from getuser into @h_user,@snumber,@h_factory,@h_factory_id,@htype
end
close ’存储名称‘
deallocate ’存储名称‘end