即时同步两个表的实现方法
测试环境:SQL2000,远程主机名:xz,用户名:sa,密码:none,数据库名:test
-创建一个测试表,不能使用标识列作为主键,因为不能正常更新
-在本地计算机上创建一个测试表,在远程主机上做同样的建表操作。只有触发器
if存在(select * from dbo.sysobjects,其中id = object _ id (n' [test]')和object property (id,N ' I supertable ')= 1)
drop table[test]
Create table test(id int not null constraint PK _ test primary key
,Name varchar(10)]
Go
-Create synchronous trigger
Create trigger t t on test
for insert、update..XP _ cmdshell ' isql/s " xz "/u " sa "/p " "/Q " exec master..XP _ cmdshell'' net start MSDTC ' ',no _ output " ',no _ output
-启动本地MSDTC服务
execmaster..XP _ cmdshell' net start msdtc ',No _ output
-考试提示:进行分布式事务处理。如果该表使用ID列作为主键,请使用以下方法
Begin distributed transaction
从Openrowset中删除(' sqloledb ',' xz ');'sa ';',test . dbo . test)
where id in(select id from deleted)
insert into openrowset(' SQL oledb ',' xz ';'sa ';,test . dbo . test)
Select * from inserted
commit tran
go
-insert data test
insert into test
Select 1,' aa'
union all select 2,' bb'
union all select 3,' c'
union all select 4,' dd'
union all select 5,AB'
Union All Select 6,' BC'
Union All Select,test.dbo.test) b on a.id=b.id
0条评论