日期:2014-05-19  浏览次数:20639 次

请问如何使用openrowset来根据本地数据来更新远程数据?
目前我要做一个操作,就是将远程数据导入本地数据,然后再删除远程数据中已经导入的记录。

本地数据表和远程数据表的结构一样的,使用相同的主键。

但我在使用openrowset语句的过程中有点问题:

下面这句将数据从远程导到本地的没有问题

insert   into   T_HR192   (

SK01,
SK02,
SK03,
SK04,
SK05,
SK06,
SK07,
SK08,
SK09,
SK10,
SK11,
SK12
)
select  
rtrim(abc.SK01),
rtrim(abc.SK02),
rtrim(abc.SK03),
rtrim(abc.SK04),
rtrim(abc.SK05),
rtrim(abc.SK06),
rtrim(abc.SK07),
rtrim(abc.SK08),
rtrim(abc.SK09),
rtrim(abc.SK10),
rtrim(abc.SK11),
rtrim(abc.SK12)
from   openrowset( 'SQLOLEDB ', 'servername '; 'sa '; 'password ',ID_Eatery.dbo.S_SK)   abc
left   join  
T_HR192   abd
on   abc.SK03   =   abd.SK03
and   abc.SK07   =   abd.SK07
and   abc.SK08   =   abd.SK08
where  
abd.SK03   is   null  
and   abd.SK07   is   null  
and   abd.SK08   is   null
and   abc.SK07   >   cast((@startday   +   '   15:30:00 ')   as   datetime)
and   abc.SK07   <=   @today
order   by  
abc.SK07   asc,  
abc.SK08   asc,  
abc.SK03   asc

但是删除远程数据有点问题

delete   openrowset( 'SQLOLEDB ', 'servername '; 'sa '; 'password ',ID_Eatery.dbo.S_SK)   abc
from   T_HR192   abd
where   on   abc.SK03   =   abd.SK03
and   abc.SK07   =   abd.SK07
and   abc.SK08   =   abd.SK08

请问怎么通过本地数据与远程数据的比对删除远程数据???


------解决方案--------------------
select * into #t from openrowset( 'sqloledb ', 'ip '; 'sa '; ' ',select * from dbo.t)a


delete
openrowset( 'sqloledb ', 'ip '; 'sa '; ' ',select * from dbo.t)a
where exists (select 1 from #t where a.id=id)
------解决方案--------------------
select * into #t from openrowset( 'sqloledb ', 'ip '; 'sa '; ' ',select * from 數據庫.dbo.t)a


delete
openrowset( 'sqloledb ', 'ip '; 'sa '; ' ',select * from 數據庫.dbo.t)a
where exists (select 1 from #t where a.id=id)