求SQL语句实现不同服务器之间更新数据问题!必给分 ( 积分: 50 )

  • 主题发起人 主题发起人 zhengjp
  • 开始时间 开始时间
Z

zhengjp

Unregistered / Unconfirmed
GUEST, unregistred user!
求SQL语句用SQL实现不同服务器之间更新数据问题!必给分<br><br>有两台服务器(A、B),把服务器A中的A1数据库A11表的数据更新到<br>服务器B中的B1数据库B11表,当B11表中不存在A11的数据时,最好是<br>触发器!必给分,谢谢!
 
建立链接服务器,再表触发器里写更新SQL,记着SET&nbsp;XACT_ABORT&nbsp;ON;这样才允许更新;
 
function&nbsp;DownloadTable(RemoteDataSet,&nbsp;LocalDataSet:&nbsp;TCustomADODataSet):&nbsp;Boolean;<br>var<br> I,&nbsp;FieldCount:&nbsp;Integer;<br>&nbsp;&nbsp;Error:&nbsp;String;<br>begin<br> Result&nbsp;:=&nbsp;true;<br> RemoteDataSet.First;<br>&nbsp;&nbsp;FieldCount&nbsp;:=&nbsp;RemoteDataSet.FieldCount;<br>&nbsp;&nbsp;EmptyTable(LocalDataSet);<br><br>&nbsp;&nbsp;while&nbsp;not&nbsp;RemoteDataSet.Eof&nbsp;do&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;LocalDataSet.Append;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;I&nbsp;:=&nbsp;0&nbsp;to&nbsp;FieldCount&nbsp;-&nbsp;1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp; if&nbsp;LocalDataSet.Fields.FieldName&nbsp;=&nbsp;RemoteDataSet.Fields.FieldName&nbsp;then<br>&nbsp;&nbsp; LocalDataSet.Fields.AsString&nbsp;:=&nbsp;RemoteDataSet.Fields.AsString<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Error&nbsp;:=&nbsp;Format('%s与%s字段不匹配。',&nbsp;[RemoteDataSet.Fields.FieldName,&nbsp;LocalDataSet.Fields.FieldName]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application.MessageBox(PChar(Error),&nbsp;'');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;false;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;LocalDataSet.Post;<br>&nbsp;&nbsp;&nbsp;&nbsp;RemoteDataSet.Next;<br>&nbsp;&nbsp;end;&nbsp;{&nbsp;end&nbsp;while&nbsp;}<br>end;
 
这样的SQL语句!没有高手吗!测试成功必给分!给个思路~谢谢
 
请使用<br>OPENDATASOURCE('SQLOLEDB','Data&nbsp;Source=服务器B的地址;User&nbsp;ID=用户名;Password=密码').数据库.dbo.表<br>其它操作就一样的了,直接在服务器A写解发器即可,比如新增一条记录到B服务器B中的B1数据库B11表<br>insert&nbsp;into&nbsp;OPENDATASOURCE('SQLOLEDB','Data&nbsp;Source=服务器B的地址;User&nbsp;ID=用户名;Password=密码').B1.dbo.B11(字段列表)&nbsp;values(值列表)
 
在触发器中写出入如下代码:<br>delete&nbsp;from&nbsp;B.B1.B11&nbsp;where&nbsp;关键字&nbsp;in&nbsp;(select&nbsp;关键字&nbsp;from&nbsp;deleted)<br>insert&nbsp;into&nbsp;B.B1.B11&nbsp;select&nbsp;*&nbsp;from&nbsp;inserted<br>给分吧!
 
TO:dyb_23&nbsp;兄弟有这种写法(B.B1.B11&nbsp;)吗。。。我测试没有成功!<br>不过还是感谢你的回贴。。答对才给分哦<br>TO:hfqxfx<br>我这样写:select&nbsp;*&nbsp;from&nbsp;OPENDATASOURCE('SQLOLEDB','Data&nbsp;Source=10.0.2.66;User&nbsp;ID=sa;Password=').studten.dbo.t_s1<br>提示:Login&nbsp;failed&nbsp;for&nbsp;user&nbsp;'sa'.&nbsp;Reason:&nbsp;Not&nbsp;associated&nbsp;with&nbsp;a&nbsp;trusted&nbsp;SQL&nbsp;Server&nbsp;connection.<br>这是什么原因?
 
TO:hfqxfx<br>你的结果我已经测成功了。。thanks&nbsp;ALL
 
多人接受答案了。
 
后退
顶部