首先要知道同步包括三种操作:insert、update、delete,delete比较少见,这里不作介绍。
1、不存在的情况下插入记录
insert into opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...tableA(field1,field2…)
select field1,field2… from tableA where id not in (select id from opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...tableA)
需要注意的是sql server数据库中的bit类型和access中bit类型的true值不一样,在sql server中true是1,而access中true是-1,需要用case语句判断一下,如:
select case bitField=0 then 0 else –1 end ,… from tableA
2、存在的情况下更新
update opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...tableA
set field1=b.field1,field2=b.field2,…
from opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...tableA a inner join tableA b on a.id=b.id
注意,下面的语句都不对:
错误语法1:
update a set a.field1=b.field1,a.field2=b.field2,…
from opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...tableA a inner join tableA b on a.id=b.id
错误语法2:
update opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...a
set a.field1=b.field1,a.field2=b.field2,…
from opendatasource('Microsoft.Jet.OLEDB.4.0','data source="D:/data/data.mdb";Jet OLEDB:DataBase Password=123')...tableA a inner join tableA b on a.id=b.id